为什么这个 mod_rewrite RewriteRule 指令在 .htaccess 文件中不起作用?

为什么这个 mod_rewrite RewriteRule 指令在 .htaccess 文件中不起作用?

我有一个托管在 Linux el cheapo 托管服务上的网站,我正在将其迁移到运行 Apache 2.2.8 和 PHP 5.2.5 w/rewrite_module 的 Mac OS X 10.5 Leopard Server 服务器AllowOverride All,但我遇到了文件中以下几行的问题.htaccess

RewriteEngine On
#RewriteRule ^view/([^/\.]+)/?$ /view.php?item=$1 [L]
#RewriteRule ^order/([^/\.]+)/?$ /order.php?item=$1 [L]
RewriteRule ^category/([^/\.]+)/?$ /category.php?category=$1 [L]

如您所见,我已注释掉和RewriteRule的指令,因此我仅处理。当我尝试加载时,它会运行(我已添加调试代码以确认),但通过 和 时为空。/view//order//category/http://domain.tld/category/2/category.php$_SERVER['REQUEST_URI']/category/2/$_GET['category']

我通常可以顺利地解决 .htaccess 文件和 mod_rewrite 指令的故障,但是这个不知为何却让我很为难。

更新: 我跟着Josh 的建议mod_rewrite.log以下是我尝试访问时转储的内容http://domain.tld/category/2/

65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (2) init rewrite engine with requested uri /category/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) applying pattern '.*' to uri '/category/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (1) pass through /category/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] add path info postfix: /Library/WebServer/Documents/tld.domain.www/category.php -> /Library/WebServer/Documents/tld.domain.www/category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/category.php/13 -> category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri 'category.php/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6aa98/subreq] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/category.php
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] add path info postfix: /Library/WebServer/Documents/tld.domain.www/category.php -> /Library/WebServer/Documents/tld.domain.www/category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/category.php/13 -> category.php/13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri 'category.php/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b5ea98/initial] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/category.php
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (2) init rewrite engine with requested uri /13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) applying pattern '.*' to uri '/13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (1) pass through /13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] strip per-dir prefix: /Library/WebServer/Documents/tld.domain.www/13 -> 13
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (3) [perdir /Library/WebServer/Documents/tld.domain.www/] applying pattern '^category/([^/\.]+)/?$' to uri '13'
65.19.81.253 - - [22/Oct/2009:17:31:53 --0400] [domain.tld/sid#100aae0b0][rid#100b6ea98/subreq] (1) [perdir /Library/WebServer/Documents/tld.domain.www/] pass through /Library/WebServer/Documents/tld.domain.www/13

答案1

只需将其添加到你的 .htaccess 文件中

Options -MultiViews

我已经遇到同样的问题好几个小时了

我不知道为什么,但它在 OSX 上默认开启

它会产生错误,因为当您尝试加载时类别/它发现类别.php并将斜杠后的所有内容重定向到该文件

答案2

您有权访问 httpd.conf 吗?如果可以,请将以下内容添加到 httpd.conf:

RewriteLog "/tmp/mod_rewrite.log" 
RewriteLogLevel 3

并在加载时发布该日志的内容/category/2。从中获取所需信息后将其删除 - 这非常浪费服务器资源。

编辑:

感谢日志。有些东西正在将 URL 重写为:/category.php/13。还有其他 RewriteRules 吗?您使用的是 FastCGI 吗?如果您将其重命名category.php为其他名称(例如Category.php或),会怎么样display_category.php

编辑2:

由于某些内容似乎正在重写/category/(.*)/category/$1而我们不知道它是什么,为什么不更改:

RewriteRule ^category/([^/\.]+)/?$ /category.php?category=$1 [L]

到:

RewriteRule ^category.php/([^/\.]+)/?$ /category.php?category=$1 [L]

看看是否有效?

答案3

我可能有点傻,但这不是预期的行为吗? Mod_rewrite 会更改用于查找文件的内联 URL,而 PHP 看到的环境变量通常不会改变。

我认为您必须解析 REQUEST_URI 字符串才能提取类别变量。

标记

相关内容