Apache's mod_rewrite failing to remove trailing slash

廉价感情. 提交于 2020-01-07 03:36:06

问题


I have a XAMPP Apache server, and have added a configuration file to rewrite URLs with a trailing slash and redirect to their slash-less counterparts.

Therefore, a url like http://example.com/the-audio/ gets redirected to http://example.com/the-audio. The problem is, it's not working when the directory name is only one word.

So, http://example.com/audio/ does not get it's slash removed. This is really strange for me, and checking the logs it appears as though the rule isn't matched in this case.

Here's my rule (only one in the file)

.htaccess

RewriteEngine on

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

And this is the error dump

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

Thanks in advance!


回答1:


According to your log /audio/ is an existing directory. So your rule wouldn't run since the first condition stops it running on existing directories. Remove the first condition to resolve it.

Also, to do this on existing directories, you will need to turn DirectorySlash off, or the server will add the slash back in itself. Note that there is some security concern with that.



来源:https://stackoverflow.com/questions/41603578/apaches-mod-rewrite-failing-to-remove-trailing-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!