.htaccess 似乎忽略了 RewriteCond
这是我的 htaccess: 选项 +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pages
RewriteRule ^(.+)/$ /$1 [NC]
RewriteRule ^$ /pages/index.php [NC]
RewriteRule ^([a-z]+)\?([^/]+)$ /pages/$1.php?$2 [NC]
RewriteRule ^([a-z]+)/([^/]+)$ /pages/$1.php?q=$2 [NC]
RewriteRule ^([a-z]+)$ /pages/$1.php [NC]
ErrorDocument 404 /pages/404.php
应该做的事情非常简单:
- 从 URL 中删除尾部斜杠
- 直接 example.com/tomato 到 example.com/pages/tomato.php
- 直接 example.com/tomato?c=red&size=big 到 example.com /pages/tomato.php?c=red&size=big
- 直接 example.com/tomato/red 到 example.com/pages/tomato.php?q=r
但这就是问题所在: for /pages/ 的任何 URL tomato.php 与 .htaccess 的第 7 行匹配,这将产生无限循环。这就是为什么我为 /pages 添加了一个例外,但它似乎被忽略了 - 我仍然收到 500 Internal Server Error 并显示以下日志消息:
mod_rewrite:达到内部重定向的最大数量。假设配置错误。如有必要,请使用“RewriteOptions MaxRedirects”增加限制。
这是否与我使用虚拟主机运行此程序有关?如果是这样,应该采取什么措施来解决它?
This is the htaccess I have:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pages
RewriteRule ^(.+)/$ /$1 [NC]
RewriteRule ^$ /pages/index.php [NC]
RewriteRule ^([a-z]+)\?([^/]+)$ /pages/$1.php?$2 [NC]
RewriteRule ^([a-z]+)/([^/]+)$ /pages/$1.php?q=$2 [NC]
RewriteRule ^([a-z]+)$ /pages/$1.php [NC]
ErrorDocument 404 /pages/404.php
What is should do is quite simple:
- remove trailing slash from URL
- direct example.com/tomato to example.com/pages/tomato.php
- direct example.com/tomato?c=red&size=big to example.com/pages/tomato.php?c=red&size=big
- direct example.com/tomato/red to example.com/pages/tomato.php?q=r
But this is the problem: any URL of the for /pages/tomato.php matches line 7 of the .htaccess which would produce an infinite loop. That's why I added an exception for /pages but it seems to be ignored - I still get 500 Internal Server Error with this log message:
mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.
Could this have something to do with the fact that I'm running this with virual hosts? If so, what should be done to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RewriteCond 仅适用于下一个 RewriteRule,因此您必须为每个规则复制该条件。
RewriteCond only applies to the next RewriteRule, so you have to copy that condition for every rule.
您可以在其他规则之前使用中断规则:
You could use a break rule in front of your other rules: