Forum Moderators: phranque

Message Too Old, No Replies

stacked redirects

having problems avoiding stacked redirects

         

Wayder

11:13 pm on Feb 8, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I am having problems avoiding multiple redirects. I have tried to reorder but without success.

For example http://www.example.com/index.html?wrong will redirect twice no matter which order I put the redirects in.

Can anyone help.

Also, if there is any other issues that you can see I would be grateful if you would mention it.

## REMOVE SPURIOUS QUERY STRINGS
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^?]+\? [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
#
## REDIRECT /INDEX.HTML OR DIR/INDEX.HTML TO / OR /DIR/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
#
## ADD SLASH IF DIRECTORY URI DOESN'T CONTAIN A TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
#
## REMOVE SLASHES IF URI CONTAINS MULTIPLE SLASHES
RewriteCond %{REQUEST_URI} //
RewriteRule ([^/]*)/+(.*) http://www.example.com/$1/$2 [R=301,L]
#
## REMOVE SLASHES IF URI CONTAINS MULTIPLE SLASHES BEFORE URI-PATH
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ //+([^\ ]*)
RewriteRule (.*) http://www.example.com/%1 [R=301,L]
#
## REDIRECT NON-WWW TO WWW
RewriteCond %{HTTP_HOST} !^w{3}\.example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
## REDIRECT HTTPS TO HTTP
RewriteCond %{HTTPS} =on
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#

Thanks

Ray...

jdMorgan

1:16 am on Feb 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you checked the Apache Forum Library? A Guide To Fixing Google Duplicate Content & URL Issues [webmasterworld.com]

Jim

g1smd

1:35 am on Feb 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd add a new rule right at the beginning that caters for index URLs that have parameters and make it fix both things at the same time, and force the www too.

That way, your existing 'parameter' rule would then only need to run for non-index URLs. Likewise, your index 'redirect' would run for index URLs without parameters.

There's at least one unescaped period in your code example above. That's something that I often miss, too.

.

[edit]Actually, no. Looking again. Just moving your index rule to be first, and adding a question mark to the target URL would mean it would strip parameters if present and still operate to strip the index part even if they weren't present. No need for an extra rule at all. Just need the "most specific rule first"; in this case "index" is more specific than "URL with parameters".

I would also put your "slashes" rules before the 'parameters' rule, and have those 'slashes' rules also strip out parameters if they happened to be present. Just add a question mark to the target URL in your 'slashes' rules.

All of your rules force www if the request was for non-www while doing whatever the specific rule is also meant to be doing. Make it so each one also strips parameters at the same time too.[/edit]