我需要像在.htaccess中那样在nginx中创建rewrite,有些行我还不完全理解。
DirectoryIndex index.php
RewriteEngine on
RewriteCond % !-f
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
有人能给我解释一下吗?
RewriteCond % !-f
似乎是不正确的规则条件,并且总是计算为true。
此规则:
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
是否匹配任何带有尾部斜线的URI并在内部重写为/?path=uri-without-slash
因此,例如:URI /foo/
将被重写为/?path=foo
- QSA-查询字符串追加
- L=最后一条规则
参考:Apache mod_rewrite简介
更新:将不正确的条件更改为:
# request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
# request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ ?path=$1 [QSA,L]
这意味着,如果请求不是针对文件的,则将后面的/
之前的所有内容重写为index.php?path=
,然后再重写之前匹配的内容。
它应该是最后一条规则(L
),并且应该附加查询字符串(QSA
),而不是因为替换的查询字符串而丢弃它。