我有以下mod重写:
RewriteEngine On
# rest api rewrites
RewriteCond %{REQUEST_URI} /api/v [NC]
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
# main application rewrite
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/[a-zA-Z0-9\-\_/]+?$ /index.html [L,QSA]
当我将这些放入我的vhost配置中时,一切都工作为例外但是当我把它放入我的.htaccess文件时,第一次重写会进行无限循环(日志显示内部重写),第二次重写不起作用一点都不。
为什么这些重写会在vhost中而不是我的.htaccess文件中?
答案 0 :(得分:1)
第一个规则循环,因为你的目标匹配正则表达式,所以一旦它重写第一次和规则循环,^(.*)$ matches the URI that you've just rewritten to (
/ index.php / etc ...`),所以它会导致无限循环(或循环次数配置为内部重定向限制)。您需要添加条件以防止循环:
# rest api rewrites
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /api/v [NC]
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
第二条规则根本没有得到应用,因为通过htaccess文件中的规则放置的URI会删除前导斜杠(因为htaccess本质上类似于vhost / server config中的<Directory>
),所以你需要至少使该前导斜杠可选:
# main application rewrite
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/?[a-zA-Z0-9\-\_/]+?$ /index.html [L,QSA]