Apache 的 mod_rewrite 无法删除结尾的斜杠

Apache's mod_rewrite failing to remove trailing slash

我有一个 XAMPP Apache 服务器,并添加了一个配置文件来重写带有尾部斜杠的 URL 并重定向到它们的无斜杠副本。

因此,像 http://example.com/the-audio/ 这样的 url 会被重定向到 http://example.com/the-audio。问题是,当目录名只有一个字时,它不起作用。

因此,http://example.com/audio/ 没有删除斜杠。这对我来说真的很奇怪,检查日志似乎在这种情况下规则不匹配。

这是我的规则(文件中只有一个)

.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)\/+$
RewriteRule ^ %1 [R=301,L]

这是错误转储

error.log

[Wed Jan 11 22:13:32.729812 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] strip per-dir prefix: C:/xampp/htdocs/tecnoedu/audio/ -> audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace3] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] applying pattern '^' to uri 'audio/'
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#29a2190/initial] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/
[Wed Jan 11 22:13:32.730312 2017] [rewrite:trace1] [pid 1688:tid 1904] mod_rewrite.c(477): [client ::1:52432] ::1 - - [localhost/sid#ba8340][rid#33046a0/subreq] [perdir C:/xampp/htdocs/tecnoedu/] pass through C:/xampp/htdocs/tecnoedu/audio/index.php

提前致谢!

根据您的日志,/audio/ 是一个现有目录。所以你的规则不会 运行 因为第一个条件停止它 运行 在现有目录上。删除第一个条件以解决它。

此外,要在现有目录上执行此操作,您需要关闭 DirectorySlash,否则服务器会自行添加斜杠。注意有some security concern with that.