samazi
samazi

Reputation: 1171

Apache Mod Rewrite For Front Controller Pattern

I have the following in my .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ _service.php?uri=$1 [NC,L]
</IfModule>

Only problem is there is no rewrite happening and I simply see a directory listing when attempting to access the root page of the server localhost.

Even when I try to make the match optional RewriteRule ^(.*)?$ _service.php?uri=$1 [NC,L] it does not work.

The rewrite seems fine for any other case e.g. localhost/foo/bar

How do I get the rewrite to happen for this particular case?

Upvotes: 1

Views: 1014

Answers (2)

Coder
Coder

Reputation: 3103

You might need to specify the document root in order for the file or directory to be found; my variation on this pattern is:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L,QSA,B]

The root page is served correctly (for me) in this case (e.g. http://www.example.com/). NB, I have this in vhost.conf and not in a .htaccess.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143906

The problem is that the root is a directory. So this condition is preventing your rule from getting applied:

RewriteCond %{REQUEST_FILENAME} !-d

Try adding a rule like:

RewriteRule ^$ _service.php?uri=/ [NC,L]

or something

Upvotes: 1

Related Questions