开发者

What's wrong with this mod_rewrite?

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/errors/$
RewriteCond %{REQUEST_URI} !^/content/$
RewriteRule ^(.*)$ /domain/$1/ [L]

The first and last line work, they ensure that all content from that domain goto that subdirectory. The two in the middle I am attempting to keep from redirecting. I need to access those 2 folders from the subdirectory, but the HTTP_HOST just sends it back to the same folder (error 404).

I want it to ignore that rule if it's looking for domain.com/content开发者_运维知识库/(.*) or domain.com/content/(.*). Please note that I do not have access to server config, and .htaccess is the only way I can do this.


I would suggest compacting your .htaccess code like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/*(domain|errors|content)/ [NC]
RewriteRule . /domain%{REQUEST_URI}/ [L]
  • . needs to be escaped as \.
  • [NC] is for ignore case comparison
  • Negative URI match for errors and content can be combined in single rule with OR |
  • %{REQUEST_URI} already has your original URI so no need to capture that
  • Added domain in negative match to avoid infinite looping


The regular expressions in your conditions are only matching requests for /errors/ exactly, because you have included the start-of-string and end-of-string anchors (^ and $):

RewriteCond %{REQUEST_URI} !^/errors/$

Since you want your condition to match for all requests starting with /errors/, you should get rid of the $ at the end:

RewriteCond %{REQUEST_URI} !^/errors/

The same obviously applies for the /content/ condition.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜