2013-03-13 97 views
0

我遇到了一個mod_rewrite問題,我的.htaccess文件中的第二條規則覆蓋了第一條規則。有問題的.htaccess文件看起來像下面這樣:mod_rewrite覆蓋第二個RewriteRule

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /path/appname 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule /api/v1/(.*)$ api/v1/index.php?rquest=$1 [QSA,NC,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php 
</IfModule> 

是我看到的問題是這樣的:

如果我直接去http://example.com/path/appname/api/v1/valid/endpoint第一重寫規則正確觸發和我得到的結果返回來自API。

但是,假設我訪問http://example.com/path/appname/app - 根據第二個RewriteRule重寫的頁面。此頁面向api/v1頁面發出AJAX請求。這些請求是直接通過第二個RewriteRule併發送到我的base index.php頁面。

我很困惑這可能是什麼,因爲我的理解是,[L]標誌防止任何進一步的規則一旦匹配就運行,因此一旦有任何'api/v1'的請求應該捕獲並停止檢查任何進一步的比賽。爲了正確工作,我需要更改哪些內容?

謝謝!

+0

這意味着第一個不匹配。檢查js訪問的控制檯中的確切URL,或啓用「RewriteLog」。 – Wrikken 2013-03-13 19:30:00

回答

0

您應該排除先前規則集的段路徑,以免再次處理。像這樣:

# Don't redirect/map when folders or files exist 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Exclude the previous path 
RewriteCond %{REQUEST_URI} !api/v1/? [NC] 

# Prevent loops 
RewriteCond %{REQUEST_URI} !index\.php [NC] 

RewriteRule . index.php    [L] 

</IfModule> 

用上面的行代替你問題中的最後4行。

+0

太棒了,工作。我仍然不明白爲什麼我能夠直接訪問api部分,但是從AJAX調用中重寫不能。 – lightstrike 2013-03-13 20:13:00

相關問題