2011-04-20 130 views
1

我有以下重寫規則設置:重寫規則500錯誤問題

RewriteEngine On 

RewriteRule ^api/([A-Za-z0-9-]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([A-Za-z0-9-]+)$ index.php/other/$1 [NC,L] 

不幸的是這導致我的服務器拋出一個500錯誤。單獨採取,但他們都很好,但。

我的本意是,如果該請求是http://somesite.com/api/whatever/,第一條規則將被觸發,重定向到index.php/api/whatever/

如果不是「API」的任何被作爲第二段雖然發送,它會重定向到index.php/other/whatever

我的理解有瑕疵嗎?我認爲它會在列表中下來,並且用L標誌,一旦碰到什麼就停止執行。或者我的語法錯了?

乾杯

回答

-1

我想你需要QSA flag,嘗試這樣的:(或您的系統上的等價路徑)

RewriteRule ^api/(.*)$ index.php/api/$1 [QSA,NC,L] 
RewriteRule ^(.*)$ index.php/other/$1 [QSA,NC,L] 
+0

由於沒有人評論downvote:[QSA]用於修改查詢字符串並希望原始附加到您在重寫中定義的新字符。這裏沒有查詢字符串(所以它保持不變) – 2011-04-21 09:55:25

+0

感謝您的解釋。但是我在我的本地配置上嘗試了這些重寫規則,並且只有使用QSA標誌才能正常工作。 – Bil 2011-04-21 10:05:03

2
  1. 每當你得到一個500,檢查/var/log/httpd/error_log
  2. 我很確定你的字符組中的連字符是一個正則表達式的語法錯誤。 (另外,[NC]標誌使[A-Za-z]冗餘

嘗試:

RewriteRule ^api/([-A-Z0-9]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([-A-Z0-9]+)$ index.php/other/$1 [NC,L] 

或許

RewriteRule ^api/([^/]+)$ index.php/api/$1 [NC,L] 
RewriteRule ^([^/]+)$ index.php/other/$1 [NC,L] 
+0

+1要匹配字符類中的連字符,它應該是打開後的第一個字符'[' - http://www.regular-expressions.info/charclass.html – Phil 2011-04-21 00:45:52

-1

嘗試這些規則.htaccess文件:

RewriteEngine on 
Options +FollowSymlinks -MultiViews 

RewriteRule ^api/(.*)/?$ /index.php/api/$1 [NC,L] 

RewriteCond %{REQUEST_URI} !^/index\.php/ [NC] 
RewriteRule ^(.*)/?$ /index.php/other/$1 [NC,L] 

它添加這個很重要在第二個RewriteRule之前重寫RewriteCond以避免無限重定向。