.htaccess 似乎忽略了 RewriteCond

发布于 2024-09-10 13:08:16 字数 973 浏览 2 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2024-09-17 13:08:16

RewriteCond 仅适用于下一个 RewriteRule,因此您必须为每个规则复制该条件。

RewriteCond only applies to the next RewriteRule, so you have to copy that condition for every rule.

浊酒尽余欢 2024-09-17 13:08:16

您可以在其他规则之前使用中断规则:

RewriteRule ^pages/ - [L]

You could use a break rule in front of your other rules:

RewriteRule ^pages/ - [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文