2016-12-27 91 views
0

我得到了我的基本重定向工作與mod_rewrite模塊。當請求頁面時localhost/home它正確重定向到localhost/index.php?page=home,但我遇到異常問題。添加例外mod_rewrite

我創建了一個文件夾api其中我按類別存儲文件,例如api/auth/register.phpapi/customer/create.php。我試圖使包含2個參數的重寫規則(在本例中爲驗證碼客戶),所以基本上它只是從網址中刪除.php

,我所做的規則如下

RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L] 

增加該行對我的.htaccess後,問題開始出現。例如我的.css.js文件開始重定向。所以也許我需要爲apis做一些改動?你有其他想法來改進我的重寫規則嗎?

的.htaccess提前

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^api/(.*)/(.*)/?$ api/$1/$2.php [L] # problems started to occur after adding this line 
RewriteRule (.*) index.php?page=$1 [L,QSA] 

感謝。

回答

1

RewriteCond只會影響下面的第一個RewriteRule,因此您需要將它們保留在您的初始規則旁邊,並將其添加到其上方(使用自己的條件)。 另外,您的/api規則還不夠嚴格((.*)會選擇任何內容,包括斜槓),這在您的案例中可能並不重要,但仍然存在。我sugest你試試這個:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^api/([^/]*)/([^/]*)/?$ api/$1/$2.php [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*) index.php?page=$1 [L,QSA] 
+0

它的工作正常。謝謝! – lingo