regex - htaccess 无法正常工作

标签 regex apache .htaccess mod-rewrite

在我的 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/

相关文章:

php - Symfony2 : 404 Not Found without app. php

.htaccess - 如何将所有具有匹配关键字的网址重定向到一个网址?

linux - 服务器总是在 url 中添加后缀

regex - 是否可以?匹配完全相同数量的左括号和右括号

java - 由 : java. util.regex.PatternSyntaxException 引起:非法重复

php 文件不起作用

apache - mod_rewrite 和相对 url

arrays - 组合 LEFT、SEARCH 和 REGEXREPLACE

c# - 禁止对 Dropbox 使用正则表达式的特定字符

php - .htaccess 在 SSL/端口 443 下被忽略