在我的 Web 服务器上,我有一个 htaccess 文件,如下所示:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
因此,对我的 Web 服务器的所有调用都将重定向到我的根目录中的 index.php 文件,其中包含保存所请求文件的路径参数。现在看看这两个调用:
domain.com/test/blub.php works -> path = test/blub.php
domain.com/test/ does not work -> path should be test
如何才能实现第二个调用也有效?
最佳答案
从您的规则中删除此条件:
RewriteCond %{REQUEST_FILENAME} !-d
因为这会跳过此规则中的所有实际目录。
使用您的规则:
DirectorySlash Off
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ /index.php?path=$1 [NC,L,QSA]
关于regex - htaccess 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34578354/