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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With