Forum Moderators: phranque
# remove multiple slashes anywhere in url (not working for .com//)
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . http://www.example.com%1/%2 [R=301,L]
It does 301 redirect these to example.com/test/:
example.com/test//
example.com//test//
but not these (returns a 200 and shows no change in the URL)
example.com//test/
example.com//
As far as I can see it works only if there's another double slash elsewhere in the url, but not if the only double slash is directly after the .com/
I've tried a few variations and these are the results. (Browser cache was cleared and restarted each time.)
No redirects happened even for .com//file// Header response is 200 OK:
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} //
RewriteRule ([^/]*)/+(.*) http://www.example.com/$1/$2 [R=301,L]
Can anyone spot the problem?
So we're back to looking at the actual client HTTP request header. Here's one of many ways to code it:
# Redirect to remove multiple slashes within URL-path
RewriteRule ^(([^/]+/)*)/+(.*)$ http://www.example.com/$1$3 [R=301,L]
#
# Redirect to remove multiple slashes before URL-path
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule .* http://www.example.com/%1 [R=301,L]
GET //scripts/quux.jsp?few=bars&king=kong HTTP/1.1
The rules should be used in the order shown -- As you observed, you get a "free clean-up" of leading slashes from the first rule if there are also double slashes within the URL-path.
I also built these so that they fix "more-than-one" slashes, not just double-slashes.
Not tested -- but this should give you the idea, anyway...
Jim
I ended up using a combo of the first rule I had been using and adding your second rule after it. It now removes the .com// and the file//.
It does do multiple 301s if theres a ton of slashes in the file path, but I can live with that while I try to learn how to read what that says exactly, and how to edit this a bit to get the first rule you wrote working on my server.
Thanks again for the code and the explanation. :)
RewriteRule ^/*(([^/]+/)*)/+(.*)$ http://www.example.com/$1$3 [R=301,L]
It redirected all these cases:
example.com//images//logo.gif
example.com//images/logo.gif
example.com/images//logo.gif
example.com//favicon.ico
as well as others with more directory-levels.
Jim
There's something on my server that's blocking that from working (slashes left intact, 200). I tried it on another site on a separate server and it worked like a charm. Smooth, single 301!
I'll contact my host today to see what it could be.
Thanks yet again :)
mod_speling CheckSpelling on
mod_negotiation enabled using Options +MultiViews
AcceptPathInfo on
All these can defeat mod_rewrites by "fixing-up" internal paths before mod_rewrite receives the URL.
Since the working code used THE_REQUEST (which is what the client originally sent regardless of intervening server-internal URLs rewrites/changes) it would have been unaffected by these. But my single RewriteRule *would* be affected by these.
Jim
http://www.examplewordpressblog.com/page//page/page/ It won't remove it from within it.
Also, found out that
http://www.examplewordpressblog.com/page/page/page// doesn't get fixed either.
We're working with ISAPI Rewrite v3 with htacess files. Your code works correctly for everything else. Just looks like it's fixing only the start.
Here's the code I'm using:
RewriteRule ^(([^/]+/)*)/+(.*)$ /$1$3 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule .* /%1 [R=301,L]
As stated, the short-version code I posted works for the listed examples when installed in .htaccess on an Apache server; No warranty or claim of suitability for any other application, I'm afraid. :)
Jim