Linux

為什麼這個 mod_rewrite RewriteRule 指令在 .htaccess 文件中不起作用?

  • March 22, 2012

我有一個託管在 linux el Cheapo 託管服務上的網站,我正在遷移到我的 Mac OS X 10.5 Leopard Server 伺服器,執行 Apache 2.2.8 和 PHP 5.2.5 w/rewrite_module 啟用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]

如您所見,我已經註釋掉了and的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

只需將其添加到您的 .htaccess 文件中

Options -MultiViews

我幾個小時都遇到同樣的問題

我不知道為什麼,但它在 OSX 上預設打開

它會產生您的錯誤,因為當您嘗試載入category/時,它會找到category.php並將斜杠後的所有內容重定向到該文件

您可以訪問 httpd.conf 嗎?如果是這樣,請添加到 httpd.conf:

RewriteLog "/tmp/mod_rewrite.log" 
RewriteLogLevel 3

並在您載入時發布該日誌所說的內容/category/2。在您從中獲得所需資訊後將其刪除 - 這非常浪費伺服器資源。

編輯:

感謝您的日誌。某些東西正在將 URL 重寫為:/category.php/13。還有其他 RewriteRules 嗎?你在使用 FastCGI 嗎?如果您重命名category.php為其他名稱,例如Category.phpordisplay_category.php怎麼辦?

編輯2:

既然有些東西似乎正在重寫/category/(.*)/category/$1我們不知道它是什麼,你為什麼不改變:

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

到:

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

看看這是否有效?

引用自:https://serverfault.com/questions/77347