Search code examples
regexapache.htaccessmod-rewrite

.htaccess rewrite regex: match anything but the other rules must be not affected


I have this .htaccess I want when I enter exemple.com/home it will open file home.php and login same thing else it will open hani.php

RewriteRule ^home$  home.php [L]
RewriteRule ^login$  login.php [L]
...
RewriteRule ^([^\/]+)$  hani.php?link=$1 [L]

My problem When I go home or any other it will open hani.php

Please I want a good solution because I have many pages


Solution

  • Problem is that your last rewrite is unconditional and is rewriting after other rewrites before that have finished thus sending everything to hani.php.

    There are 2 solutions (use any one):

    1. Exclude all files and directories from last rule using:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ hani.php?link=$1 [L,QSA]
    
    1. Exclude dot from your matching patter in last rule so that home.php or any request containing dot doesn't match the pattern:
    RewriteRule ^([^/.]+)/?$ hani.php?link=$1 [L,QSA]
    

    Please note that there is no need to escape / in regex patter in mod_rewrite.