php.htaccess

.htaccess map to /books or /books/ or /books/<url_slug>


I am trying to map.htaccess to /books or /books/ or /books/<url_slug>:

Options All -Indexes

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1 [R=301,L]

# Map /books/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books$ book.php [NC,L,QSA]

Result:

enter image description here

book.php:

<?php

echo '<h1>Books</h1>';

?>

Solution

  • Solved yesterday.

    Options All -Indexes
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/$ index.php [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^books/?$ book.php [QSA,NC,L]
    RewriteRule ^books/([^/]*)?$ book.php?title=$1 [QSA,NC,L]