homepage Welcome to WebmasterWorld Guest from 54.205.122.62
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Dealing with canonical problems
Removing double slash in url causing Redirect Loop
Bilbo



 
Msg#: 3802658 posted 5:01 pm on Dec 8, 2008 (gmt 0)

Hi all I wanted opinion on this.

To prevent Canniocal Issue caused by double or more slashs in url I added this rule

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

Howether if I really hammer server ie.

[yourdomain.com...]

I can cause a redirection loop and get a error code in browser.

"Redirect Loop

Redirection limit for this URL exceeded."

Additionally for each // its redirecting to /

Is there a better way to do this that can just redirect mutiple /// in url to / and not cause a redirection loop.

Any help gladly appreciated

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3802658 posted 5:32 pm on Dec 8, 2008 (gmt 0)

Allow your rule to correct *more* than two consecutive slashes by adding the "+" quantifier, which means "one or more."

RewriteCond %{REQUEST_URI} ^(.*)/[b]/+[/b](.*)$
RewriteRule . http://www.example.com%1/%2 [R=301,L]

Also, always include the canonical hostname (as shown) in all external redirects to avoid problems on servers configured with "UseCanonicalName on" and a hosting-company-configured ServerName which differs from your preferred canonical domain name.

Your code was looping even with three slashes, but three redirects are not enough to trigger a redirection limit warning with default server and browser configurations.

Jim

[edited by: jdMorgan at 5:34 pm (utc) on Dec. 8, 2008]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3802658 posted 5:51 pm on Dec 8, 2008 (gmt 0)

I should point out that even the modified code won't properly handle multiple slashes at the beginning of the URL-path. To fix those, you'll likely need two rules:

RewriteCond %{REQUEST_URI} ^//+(.*)$
RewruteRule . http://www.example.com/%1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(.+)//+(.*)$
RewriteRule . http://www.example.com/%1/%2 [R=301,L]

Jim

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3802658 posted 5:59 pm on Dec 8, 2008 (gmt 0)

I remembered something about this problem...

You might also want to test this single-line-fix rule that I found in an older thread [webmasterworld.com]:

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

Jim

Bilbo



 
Msg#: 3802658 posted 6:05 pm on Dec 8, 2008 (gmt 0)

Hi Jim,

Thanks for your help.I still seem to get it looping. Also still making lotts of redirects before it gets to the correct page.

Here are the live headers (changed domain to example.com - imagine the pagerank if I owned example.com :) )

http://www.example.com///////////////

GET /////////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:32 GMT
Server: Apache
Location: http://www.example.com//////////////
Content-Length: 250
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com//////////////

GET ////////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:32 GMT
Server: Apache
Location: http://www.example.com/////////////
Content-Length: 249
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com/////////////

GET ///////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:32 GMT
Server: Apache
Location: http://www.example.com////////////
Content-Length: 248
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com////////////

GET //////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:32 GMT
Server: Apache
Location: http://www.example.com///////////
Content-Length: 247
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com///////////

GET /////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com//////////
Content-Length: 246
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com//////////

GET ////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com/////////
Content-Length: 245
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com/////////

GET ///////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com////////
Content-Length: 244
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com////////

GET //////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com///////
Content-Length: 243
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com///////

GET /////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com//////
Content-Length: 242
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com//////

GET ////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com/////
Content-Length: 241
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com/////

GET ///// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com////
Content-Length: 240
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com////

GET //// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com///
Content-Length: 239
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com///

GET /// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com//
Content-Length: 238
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://www.example.com//

GET // HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 17:56:33 GMT
Server: Apache
Location: http://www.example.com/
Content-Length: 237
Connection: close
Content-Type: text/html; charset=iso-8859-1

Bilbo



 
Msg#: 3802658 posted 6:24 pm on Dec 8, 2008 (gmt 0)

Hi Jim, Just tried your other method

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

Works perfectly! thankyou again Jim for all your help

These were the response headers

http://www.example.com//////////////////////

GET ////////////////////// HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Mon, 08 Dec 2008 18:20:39 GMT
Server: Apache
Location: http://www.example.com/
Content-Length: 237
Connection: close
Content-Type: text/html; charset=iso-8859-1

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3802658 posted 6:51 pm on Dec 8, 2008 (gmt 0)

Good. Now try it with multi-slashes in the middle and at the end of the URL. Test all possible cases.

You will still ge multiple redirects with a URL like example.com//foo//bar//page.html because the code only fixes multiple slashes when they are contiguous in the URL. The solution to that is rather ugly, due to a bug in all versions of mod_rewrite. However, it's rare that you'll get requests for multi-position multi-slash URLs, so addressing that may not be worth the bother.

Jim

Bilbo



 
Msg#: 3802658 posted 12:34 am on Dec 9, 2008 (gmt 0)

Hi Jim for the site in question it had very short urls and no directories so pages were all http://www.example.com/page.html format.

I would love to revisit this with a directory based site but right now have to try and put some money on the table to feed the kids so have to push on. Now the rewrite rule I eventually ended up with based on your ideas was this:

RewriteCond %{REQUEST_URI} ^//+(.*)$
RewriteRule . http://www.example.com/%1 [R=301,L]

This showed only one redirect in headers and stopped the looping.I needed to use the REQUEST to prevent it conflicting another rule. Thankyou soo much for your help Jim and hopefully can revisit this soon and look at // within directories.

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