将异常添加到mod_rewrite中



我使我的基本重定向与mod_rewrite模块一起使用。请求页面时localhost/home它正确地将其重定向到localhost/index.php?page=home,但是我有例外的问题。

我创建了一个文件夹api,我按类别存储文件,例如api/auth/register.phpapi/customer/create.php。我尝试制作包含2个参数的重写规则(在此示例中 auth customer ),因此基本上它只是从URL中删除了.php

我制定的规则正在遵循

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

将该行添加到我的.htaccess后,开始出现问题。例如,我的.css.js文件开始重定向。所以也许我需要对API进行一些侵害?您还有其他一些想法来改善我的重写规则吗?

.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]

预先感谢。

RewriteCond只会影响第一个RewriteRule,因此您需要将它们保持在初始规则旁边,然后将添加的一个添加到它们上方(具有其自身条件)。另外,您的/api规则不够严格((.*)会选择任何内容,包括斜线),这在您的情况下可能并不重要,但仍然如此。我很喜欢您尝试一下:

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]

最新更新