Apache mod_rewrite for everything except root

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

I have now

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

And this loads index.html perfectly, but the / tag is not passed to webroot. I get a 404 error.

thanks

+6
apache .htaccess mod-rewrite
source share
1 answer

This condition is a problem:

 RewriteCond %{REQUEST_URI} !^/ 

You say that everything that starts with '/' is not overwritten, and everything starts with '/'. You should use $ at the end:

 RewriteCond %{REQUEST_URI} !^/$ 

I'm not sure if you need a rule at all, because if index.html exists, the other two rules will take care of this automatically. Just use them to rewrite everything that does not physically exist:

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

And you can handle the 404 error in your application, since in any case you will need it for subdirectories.

+7
source share

All Articles