skip to Main Content

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>';

?>

2

Answers


  1. Chosen as BEST ANSWER

    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 ^posts/?$ post.php [QSA,NC,L]
    RewriteRule ^posts/([^/]*)?$ post.php?title=$1 [QSA,NC,L]
    

  2. You are just making your life more complicated.

    The general advice is to avoid use of url-rewrites for specifying paths. Instead you should just have something like:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ /index.php [L,QSA]
    

    And use $_SERVER['REQUEST_URI'] on the PHP side to then route the incoming call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search