Home > database >  Removing Double Slashes From URL By .htaccess does not work
Removing Double Slashes From URL By .htaccess does not work

Time:10-08

How come that none of these solutions work on my Apache servers:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=302,L]

or

RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=302,L]

or

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=302,L]

among others that I tried.

I tried ALL solutions from this page: Issue In Removing Double Or More Slashes From URL By .htaccess

and other pages as well.

The problem is that the rule in the htaccess does not match the double slashes within these above patterns.

I tried also "literal" patterns, with exact urls without regex patterns. Still nothing. But with a single slash - all work.

It seems like Apache has a problem when it finds: "//" - the url is clearly not recognized and the rule is ommited.

The idea is simple: to get rid of double slashes and replace them with one slash:

 http://demo.codesamplez.com/html5//audio -> http://demo.codesamplez.com/html5/audio

Do you know how can I redirect URL with double slash "//" to a single onen "/"?

Here's htaccess (removed the longest comments in the file):

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On


RewriteCond %{REQUEST_URI} ^/test//slash
RewriteRule ^(.*)$ /test/slash [R=302,L]


RewriteCond %{REQUEST_URI}::$1 ^(/. )/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite all other queries to the front controller.
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>

CodePudding user response:

Try the following instead:

# Remove multiple slashes anywhere in the URL-path
RewriteCond %{THE_REQUEST} \s[^?]*// 
RewriteRule (.*) /$1 [R=302,L]

This uses the fact that multiple slashes have already been reduced in the URL-path matched by the RewriteRule pattern. And the check against THE_REQUEST (which contains the first line of the request headers and does not change throughout the request) ensures that multiple slashes were initially present somewhere in the URL-path (excluding the query string).

Another potential issue is if you have a proxy server (or load balancer) in front of your application (Apache) server and this is perhaps normalizing the request (reducing multiple slashes, removing trailing space, etc) as it forwards the request to your application (Apache) server. The application server then never sees the original request (with multiple slashes) that you see in the browser.


Looking at your attempts...

RewriteCond %{REQUEST_URI} ^/test//slash
RewriteRule ^(.*)$ /test/slash [R=302,L]

This "should" work, with the limited example as posted. However, the REQUEST_URI server variable is modified throughout the request, so if the URL has already been modified (perhaps in the server config) then this may not match.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=302,L]

This only matches multiple slashes at the start of the URL-path, not anywhere in the URL-path. This would also result in a malformed redirect if used in .htaccess (unless you also had a RewriteBase directive set). Without the slash prefix on the substitution string this rule is probably intended for a server or virtualhost context, not .htaccess.

RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=302,L]

The same issue with the use of REQUEST_URI as mentioned above. Otherwise, this should work. However, it results in multiple redirects if there are more than 1 group of multiple slashes. eg. //foo//bar.

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=302,L]

The same as above, except this only matches double slashes, rather than groups of two or more slashes. So if there are more than two slashes in a group it will result in multiple redirects.

  • Related