Forum Moderators: phranque

Message Too Old, No Replies

Chaining 301 redirects AND SEO

Chaining 301 redirects AND SEO

         

Bogus

8:33 pm on Sep 23, 2010 (gmt 0)

10+ Year Member



Hi there,

I'm trying to prevent duplicate content on my site. So i have my .htaccess setup to add www and trailing slash if not used.

Upon accessing mywebsite.nl/test 2 redirects will follow. First one to add the www part, next one to add the trailing slash.

I'm afraid google will not accept 2 redirects to find the definitive content.

Can this be done with 1 rewrite rule ?

.htacces snippet:

#add WWW and redirect
RewriteCond %{HTTP_HOST} !^www.mywebsite.nl$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.nl/$1 [L,R=301]

#add trailing slash & prevent duplicate content
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.mywebsite.nl/$1/ [L,R=301]


Any experts on this ?

g1smd

8:44 pm on Sep 23, 2010 (gmt 0)

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



The double redirect is bad news and must be avoided.

Yes, it can and should be done, but one other thing to note is that a URL with a trailing slash represents a folder, or the index page of a folder. URLs for pages should not have a trailing slash.

Additionally, your -f "exists" check is very inefficient and will hammer your hard drive, while forcing an early server upgrade. This check currently runs for every request arriving at your server. There should be a preceding RewriteCond that eliminates this check for the all requests that will never be serviced by the RewriteRule.

Rule order is important, the non-www to www redirect should always be the last in the list of redirects.

Literal periods in patterns should always be escaped.

Bogus

9:09 pm on Sep 23, 2010 (gmt 0)

10+ Year Member



I was thinking that the non-www rule would be applied much more often then the trailing slash rule, so that would be the first.

But now comes the big question, how can this be done with one redirect ? :)

g1smd

10:26 pm on Sep 23, 2010 (gmt 0)

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



Change the order of the redirects. That solves the problem.

The trailing slash redirect already fixes the www in the same redirect.

That's why the non-www to www redirect is listed last - it only needs to run if none of the other redirects have been activated.

The other redirects also fix the www at the same time as whatever else it is they do.

Bogus

8:06 am on Sep 24, 2010 (gmt 0)

10+ Year Member



ok, what was i thinking :)

you're the best, works like a charm

in case somebody want's to see the chain, i have found this useful site
[blog.cartercole.com...]

jdMorgan

11:37 am on Sep 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With a few more optimizations, corrections, and accurate comments:

# Externally redirect to add missing trailing slash if requested URL-path does not resolve to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://www.mywebsite.nl/$1/ [L,R=301]
#
# Externally redirect all non-blank non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.mywebsite\.nl)?$
RewriteRule ^(.*)$ http://www.mywebsite.nl/$1 [L,R=301]

Jim