homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

redirecting with double forward slash in URL
mod rewrite

 8:48 am on Jan 22, 2013 (gmt 0)

I got an alarming number of pages not found at my site because of presence of two forward slashes in the URL. I am unable to located the exact source of the link.
Here is the example.


This should be redirected as


I have one .htaccess file at root of my site which redirects example.com to www.example.com. ( this is working )
I have added these few lines to it ( learnt from here one old post ) but it is not working, cleared my cache also and tried.
Please help me , what is wrong here.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Externally redirect to remove multiple contiguous slashes embedded in URL
RewriteCond %{REQUEST_URI} ^/([^/]+)//+(.*)$
RewriteRule // http://www.example.com/%1/%2 [R=301,L]

I am keeping this file at root of the site. Should I keep each one at every dir ?



 10:17 am on Jan 22, 2013 (gmt 0)

The "remove slashes" rule MUST be listed before the non-www/www rule, otherwise non-www requests with double slash will go through two redirects before arriving at the correct URL.

More general purpose:
RewriteRule ^(([^/]+/)*)/+(.*) http://www.example.com/$1$3 [R=301,L]

This catches double slash anywhere in the path part of the URL request.

My preferred method is to add:
RewriteCond %{THE_REQUEST} !//
to the non-www/www rule and then add this separate rule near the start of the htaccess file:
RewriteRule // /fix-slashes.php [L]

The PHP script detects the requested URL and then does a simple PREG_REPLACE on it and sends the correct 301 HEADER back out.

Change your non-www/www rule to this:
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

as it catches more non-canonical URLs than your existing rule.


 10:26 am on Jan 22, 2013 (gmt 0)

More general purpose:
RewriteRule ^(([^/]+/)*)/+(.*) http://www.example.com/$1$3 [R=301,L]

What do you do if $3 is "index.html" or equivalent? Normally the index-redirect would be your second-to-last rule, but here you've got to intercept it or you're right back in double-redirect territory.

:: unhappily looking at timestamp and realizing that I probably didn't even need to look at adjoining post that I just finished answering ::

If I were writing the rule I'd say
leading to
but that may have more to do with individual coding preferences.


 5:36 pm on Jan 23, 2013 (gmt 0)

Sorry, Dont know why nothing is working for me, tried all codes from here and other places.

Just replaced my domain name with example.com here

Options +FollowSymLinks
RewriteEngine on
# Externally redirect to remove multiple contiguous slashes at beginning or end of URL
RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*/)/+$
RewriteRule / http://www.example.com/%1 [R=301,L]
# Externally redirect to remove multiple contiguous slashes embedded in URL
RewriteCond %{REQUEST_URI} ^/([^/]+)//+(.*)$
RewriteRule // http://www.example.com/%1/%2 [R=301,L]
# Externally redirect non-canonical domain requests to canonical domain.
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I must be doing very stupid mistake but not able to located it. Please help.


 12:35 am on Jan 24, 2013 (gmt 0)

You need to get the double slashes into the body of the Rule so your server doesn't get dragged down checking every single request. That's why you use

RewriteRule ^((?:[^/]+/)*)/+(.*) http://www.example.com/$1$2 [R=301,L]

immediately preceded by

RewriteRule ^((?:[^/.]+/)*)/+((?:[^/.]+/)*)index\.html http://www.example.com/$1$2 [R=301,L]

As always, DO NOT cut & paste. I'm typing this off the top of my head.

Matter of fact, you can probably constrain the second rule (the one I wrote first) to


(note closing anchor) replacing .html with whatever page extension(s) you really use. Calls to stylesheets, images etc come from within the page, so there's no place for the double-slashes to come from. I assume you don't want to redirect hotlinkers ;)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved