开发者

Apache mod_rewrite for everything except root

开发者 https://www.devze.com 2023-02-19 15:42 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消