2016-12-27 29 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