一些重写规则不起作用(.htaccess)

时间:2015-08-28 11:05:59

标签: php apache .htaccess mod-rewrite url-rewriting

RewriteEngine on

RewriteRule ^admin/api/(.*)$ admin/api.php?method=$1 [QSA,L]
RewriteRule ^admin/(.*)$ admin/index.php [QSA,L]

RewriteRule ^api/(.*)/$ api.php?method=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [QSA,L]

我想要管理模块的单独API入口点,但重写不能按我的意愿工作。重写规则有什么问题?

host.com/page - 工作(/index.php),

host.com/admin/page - 有效(/admin/index.php),

host.com/admin/api/section - 执行/index.php(但预计/admin/api.php)

1 个答案:

答案 0 :(得分:1)

这是因为您的规则正在循环,因为admin/.*模式也匹配admin/api.php。有这样的规则:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^admin/api/([^.]+)$ admin/api.php?method=$1 [QSA,L]

RewriteRule ^admin/([^.]+)$ admin/index.php [L]

RewriteRule ^api/([^.]+)/$ api.php?method=$1 [L]

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [L]