apache mod_rewrite 规则重定向除一个特定路径之外的所有请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mod_rewrite rule to redirect all requests except for one specific path
提问by Olly
I'm trying to redirect all requests to my domain to another domain using mod_rewrite in an Apache 2.2 VirtualHost declaration. There is one exception to this -- I'd like all requests to the /audiopath notto be redirected.
我正在尝试使用 Apache 2.2 VirtualHost 声明中的 mod_rewrite 将所有对我的域的请求重定向到另一个域。对此有一个例外——我希望所有对该/audio路径的请求都不会被重定向。
I've written a RewriteCond and RewriteRule to do this but it's not quite right and I can't figure out why. The regular expression contains a negative lookahead for the string "/audio", but for some reason this isn't matching. Here's the definition:
我已经写了一个 RewriteCond 和 RewriteRule 来做到这一点,但它不太正确,我不知道为什么。正则表达式包含对字符串“/audio”的否定前瞻,但由于某种原因,这不匹配。这是定义:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*\.)?mydomain\.net(?!/audio) [NC]
RewriteRule ^(.*)$ http://www.newdomain.net [L,R=301]
If I change the RewriteCond to:
如果我将 RewriteCond 更改为:
RewriteCond %{HTTP_HOST} ^(.*\.)?mydomain\.net/(?!audio) [NC]
(i.e. put the forward slash outside of the negative lookahead part) then it works, but the downside of this is that requests to mydomain.net without a trailing slash will not be redirected.
(即,将正斜杠放在负前瞻部分之外)然后它可以工作,但这样做的缺点是对没有尾随斜杠的 mydomain.net 的请求将不会被重定向。
Can anyone point out what I'm doing wrong?
谁能指出我做错了什么?
(Note: the angle brackets around the domain in the RewriteRule bit above are being added by StackOverflow.com -- they are not there in the actual code!)
(注意:上面 RewriteRule 位中域周围的尖括号是由 StackOverflow.com 添加的——它们在实际代码中不存在!)
Here are the rules:
以下是规则:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/mydomain.net/htdocs"
ServerName www.mydomain.net
ServerAlias mydomain.net
RewriteEngine on
RewriteCond {REQUEST_URI} !^/audio
RewriteRule ^(.*)$ http://www.newdomain.net [L,R=301]
RewriteLog logs/mod_rewrite_log
RewriteLogLevel 3
ErrorLog logs/error_log
CustomLog logs/access_log common
</VirtualHost>
Thanks @mercutio -- that makes perfect sense but it still doesn't seem to work.
谢谢@mercutio——这很有道理,但它似乎仍然不起作用。
Here's what the mod_rewrite log says when I make a request to http://mydomain.net/audio/something.mp3:
这是我向http://mydomain.net/audio/something.mp3发出请求时 mod_rewrite 日志所说的内容:
(2) init rewrite engine with requested uri /audio/something.mp3
(3) applying pattern '^(.*)$' to uri '/audio'
(2) rewrite '/audio' -> 'http://www.newdomain.net/'
(2) explicitly forcing redirect with http://www.newdomain.net
(1) escaping http://www.newdomain.net for redirect
(1) redirect to http://www.newdomain.net [REDIRECT/301]
Since the REQUEST_URI does start with /audioI would expect the RewriteRule to be ignored.
由于 REQUEST_URI 确实以/audio我希望重写规则被忽略。
回答by mercutio
The HTTP_HOST only contains the host name, not the path of the URL requested.
HTTP_HOST 只包含主机名,不包含请求的 URL 的路径。
RewriteCond %{REQUEST_URI} !^/audio
Should be all you need.
应该是你所需要的。
Further, you can get debug info from the rewrite engine with the following, which is really useful to see how your conditions and rules are being matched:
此外,您可以使用以下内容从重写引擎获取调试信息,这对于查看您的条件和规则如何匹配非常有用:
RewriteLog /path/to/log/file
RewriteLogLevel 3

