apache - .htaccess 似乎忽略了 RewriteCond

标签 apache .htaccess mod-rewrite

这是我的 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

应该做的事情很简单:

  • 删除网址尾部斜杠
  • 将 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/tomato.php 的任何 URL 都与 .htaccess 的第 7 行匹配,这将产生无限循环。这就是为什么我为/pages 添加了一个异常(exception),但它似乎被忽略了 - 我仍然收到 500 Internal Server Error 并显示以下日志消息:

mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.

这可能与我使用虚拟主机运行此程序有关吗?如果是这样,应该采取什么措施来解决它?

最佳答案

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

关于apache - .htaccess 似乎忽略了 RewriteCond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3277365/

相关文章:

.htaccess - 重写 url 以隐藏真实页面路径

php - 在 Wordpress 中将图像设为私有(private)

.htaccess:此处不允许使用 RewriteEngine

apache - .htaccess - 强制 www、尾部斜杠并删除扩展名

PHP 5.5 OPCache 不适用于 apache

php - 使用 volley 和 php 将 android 连接到 mysql 时禁止错误 403

php - 使用 MySQL 和 Apache 配置 PHP

css - Apache RewriteRule 后如何解决 CSS include 错误?

php - 只允许从表单访问 PHP 脚本,而不是直接访问

regex - 相对路径css/js的mod_rewrite问题