Search code examples
apache.htaccessmod-rewriteurl-rewritingslash

How to remove multiple trailing slashes?


Here are my rewrite rules:

###########
# Rewrite #
###########
# Settings
RewriteEngine On
RewriteBase /
# Cache Busting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} "^(.+)\.\d+\.(css|js)$" [NC]
RewriteRule "^.*$" "/%1.%2" [L]
# No Index
RewriteCond %{THE_REQUEST} "\ /.*index\.php.*\ " [NC]
RewriteRule "^(.*)index\..+$" "/$1" [L,NC,R=301]
# No Question Mark
RewriteCond %{THE_REQUEST} "\ /[^?]*\?\ "
RewriteRule "^(.*)$" "/$1?" [L,R=301]
# WWW
# RewriteCond %{HTTP_HOST} !"^(?:static|www)\.(.+)$" [NC]
# RewriteCond %{HTTPS}s "^on(s)|"
# RewriteRule "^(.*)$" http%2://www.%1/$1 [L,R=301]

Everything works fine (any suggestion to improve performances or for better regexps is welcome, anyway) but I'm experiencing a weird situation and I can't understand if it's produced by my rewrite rules or by a default Apache behavior. If my URL ends with a "/", I can append as many slashes as I want without it being rewritten.

For example, if in my address bar I insert the following:

http://[MY-HOST-NAME]////////////////////////////

All those slashes are not being removed. And I'm still seeing my index.php page. If I insert the following address:

http://[MY-HOST-NAME]/members///

All those multiple slashes are not being removed and I can see my members index.php page. And so on...

Can someone help me please? Many thanks!


Solution

  • RewriteCond %{THE_REQUEST} //
    RewriteRule .* $0 [R]