【发布时间】:2016-07-17 12:39:01
【问题描述】:
为什么会这样:
http://example.com/robots.txt
重定向到:
http://www.example.com/mvc/view/robots/live-robots.txt
遵守这些规则:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^robots.txt /mvc/view/robots/live-robots.txt [L]
#.... 20 irrelevant lines for mobile rewrites
# Force the "www."
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
但是正在加载:
http://www.example.com/robots.txt
按预期重写live-robots.txt。
L 标志不应该在这两种情况下停止重定向并且不进入后一个规则吗?
在这种情况下,L 标志可用于结束当前一轮 mod_rewrite 处理。
https://httpd.apache.org/docs/current/rewrite/flags.html
当前执行路径:
- http://example.com/robots.txt
- 301 次服务
- http://www.example.com/mvc/view/robots/live-robots.txt
然后
- http://www.example.com/robots.txt
- 200(提供 mvc/view/robots/live-robots.txt 的内容)
我很确定这不是正则表达式问题,但这里也对此进行了测试,https://regex101.com/r/eI9aC4/1。
【问题讨论】:
-
你的重写日志是什么样的?
-
从访问日志还是在某处重写特定日志?访问日志有
80 GET /robots.txt 301,然后是80 GET /mvc/view/robots/live-robots.txt 304。 -
看看the documentation(或here Apache 2.2)。
-
试试
RewriteCond %{HTTP_HOST} ^example\.com [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com(当然是在换行符之后) -
@larsks 感谢您提供该链接。这很有用,必须在
httpd.conf,而不是.htaccess。
标签: regex apache .htaccess mod-rewrite