Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache mod_rewrite for everything except root

Hi I am trying to write a mod_rewrite rule to redirect everything except the root folder. For example, www.example.com should load the index.html file For everything else, e.g. www.example.com/tag, the /tag should be passed to a script in a subdirectory

Right now I have

RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) app/webroot/$1 [L]

And that loads index.html fine but the /tag is not passed to webroot. I'm getting a 404 error.

Thanks

like image 970
sho Avatar asked Mar 25 '11 19:03

sho


1 Answers

This condition is the problem:

RewriteCond %{REQUEST_URI} !^/

You're saying anything that starts with '/' is not rewritten, and everything begins with '/'. You need to use $ at the end:

RewriteCond %{REQUEST_URI} !^/$

I'm not sure you need the rule at all, though, because if index.html exists the other two rules will take care of that automatically. Just use these to rewrite anything that doesn't physically exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ app/webroot/$1 [L,QSA]

And you can handle a 404 error in your app, since you'll have to for the subdirectories anyway.

like image 161
Kelly Avatar answered Oct 18 '22 18:10

Kelly