TheKitsuneWithATie TheKitsuneWithATie - 3 months ago 9
Apache Configuration Question

Apache - RewriteRule "$(.*)^" pattern doesn't work with empty requests

I'm trying to do some URL Rewriting with Apache2 and PHP, the problem is that I made an .htaccess file that uses rewrite_mod, here it is:

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


The
^(.*)$
pattern should match everything, but it doesn't match empty requests, and I don't know why.

To give you an exemple, here the
/root
folder is were the .htaccess file is.


  • Request
    /root/anything
    matches;

  • Request
    /root/some/thing
    matches too;

  • Request
    /root/
    doesn't match.



Here, the
/root/
request should match as the pattern
^(.*)$
means "Match any characters from 0 times to any amount of times".

Does anyone know why it doesn't match empty requests?

I'm currently running LAMP under Linux Mint.

Answer

Have your rules like this:

RewriteEngine On

# handles landing page
RewriteRule ^/?$ webroot/index.php [L]

# handles rest
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . webroot/index.php [L]

mod_dir module if runs before mod_rewrite and adds DirectoryIndex handler to landing page i.e. /index.php thus blocking execution of your rule due to RewriteCond %{REQUEST_FILENAME} !-f condition.