【问题标题】:Mod Rewrite L Flag Not stoppingMod Rewrite L Flag 不停止
【发布时间】: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

当前执行路径:

  1. http://example.com/robots.txt
  2. 301 次服务
  3. http://www.example.com/mvc/view/robots/live-robots.txt

然后

  1. http://www.example.com/robots.txt
  2. 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


【解决方案1】:

标记L 不会阻止其他规则的执行。它只是在while 循环中充当continue,并使mod_rewrite 循环再次运行。

您的规则需要按顺序颠倒。通常在内部重写之前保留重定向规则:

RewriteEngine On
RewriteBase /

# Force the "www."
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^robots\.txt$ mvc/view/robots/live-robots.txt [L,NC]
#.... 20 irrelevant lines for mobile rewrites

确保清除浏览器缓存以进行此更改。

但是,如果您有 Apache 2.4+,那么您可以使用 END 标志来完全停止以下所有规则:

RewriteRule ^robots\.txt$ mvc/view/robots/live-robots.txt [END]

【讨论】:

  • 我正在运行 2.2.15,所以我无法使用 end。 2.2 中没有办法明确地让文件停止处理?
  • No END 从 Apache 2.4+ 开始添加。对于2.2,您应该按照我的建议在重写规则之前保留重定向规则。
  • 谢谢,这回答了这个问题。正在考虑升级到 2.4。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 2012-02-14
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
相关资源
最近更新 更多