Forum Moderators: phranque

Message Too Old, No Replies

.htaccess - redirect url problem

Sequence/Order problem (I think)

         

Karma

9:57 am on Dec 15, 2009 (gmt 0)

10+ Year Member



Hi all,

I'm trying to 301 redirect an "exact url" to its new location via htaccess, however I'm also redirecting all http://www.example.com traffic to http://example.com

My .htaccess is as follows:

redirect 301 http://www.example.com/type_new/widget_blue/size_small/ http://example.com/new/blue/small/
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

For some reason, the RewriteCond/RewriteRule is picked up first, so I'm seeing multiple 301 redirects. If I comment out the RewriteCond/RewriteRule it works fine.

How do I make the redirect take preference over the RewriteCond/RewriteRule?

[edited by: jdMorgan at 7:09 pm (utc) on Dec. 15, 2009]
[edit reason] example.com [/edit]

vincevincevince

10:08 am on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Write the redirect as a RewriteRule:

RewriteRule ^type\_new\/widget\_blue\/size\_small\/$ /new/blue/small/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com.tld/$1 [R=301,L]

[edited by: jdMorgan at 7:10 pm (utc) on Dec. 15, 2009]
[edit reason] example.com [/edit]

Karma

10:24 am on Dec 15, 2009 (gmt 0)

10+ Year Member



Oops, kinda obvious when I read it!

worked a treat - thanks :)

jdMorgan

7:07 pm on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no need to escape those "/" characters in the patterns.
Do not end-anchor hostnames unless you take FQDNs and port numbers into account.
Always include the protocol and hostname in redirect rules.

So use either:


RewriteRule ^type_new/widget_blue/size_small/$ http://example.com/new/blue/small/ [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.example\.com\.?(:[0-9]+)?$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

- or -

RewriteRule ^type_new/widget_blue/size_small/$ http://example.com/new/blue/small/ [R=301,L]
#
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Jim

g1smd

11:39 pm on Dec 15, 2009 (gmt 0)

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



Those corrections rate as "important". The earlier suggestions allow duplicate content issues to creep in. The post immediately above fixes those issues.