Search code examples
apache.htaccessmod-rewritehttp-redirectdocument-root

Apache rewrite empty regex (root)


I've got Apache 2.4.4 running on Windows 8 laptop (WAMP server) and I found a really odd behavior of .htaccess mod_rewrite rules.

I want to redirect the root of my website to a specific file. My .htaccess looks like this:

RewriteEngine On
RewriteBase /
RewriteRule $^ /static/home.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]

This works only if there's no index.php file in root directory. When I create index.php file, Apache redirects empty path straight to the index file and doesn't bother with the first RewriteRule.

Is there a way to have both these RewriteRules and index.php file working together? In oher words, my example works but I want to rename router.php to index.php and keep both RewriteRules working.

Thanks!


Solution

  • You can just tweak your regex pattern to work in both situation:

    DirectoryIndex something-else.php
    RewriteEngine On
    
    RewriteRule ^$ /static/home.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]