问题
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