Forum Moderators: phranque

Message Too Old, No Replies

.htaccess help with 301 redirect

htaccess 301 redirect

         

kk4uga

6:04 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



I'm having an issue where Google is picking up some of my pages as ns1.domain.com instead of www.domain.com. I have a htaccess file (see below), but the ns1 search results do not forward to www. Any advice?


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

g1smd

7:35 pm on Jan 26, 2011 (gmt 0)

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



Your code redirects example.com (with or without a port number) to www.example.com, and does nothing for ns1.example.com requests.

kk4uga

7:40 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



Correct.

How can I alter the code to redirect example.com AND ns1.example.com to www.example.com?

Thanks!

g1smd

8:14 pm on Jan 26, 2011 (gmt 0)

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



To also redirect ns1 you could use:

RewriteCond %{HTTP_HOST} ^(ns1\.)?example\.com [NC]


What about www.ns1.example.com too? The new code does not redirect that.

If you want to redirect anything that is not exactly www.example.com then this is useful:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

[edited by: g1smd at 8:27 pm (utc) on Jan 26, 2011]

kk4uga

8:19 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



The code above is my original code. I was looking for how to edit that to include ns1.

Someone else helped me out. Here is the result:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain\.com [NC]
RewriteRule (.*) [domain.com...] [R=301,L]

Now the ns1 redirects to www.

g1smd

8:30 pm on Jan 26, 2011 (gmt 0)

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



The latest code will not redirect www.example.com:80 requests with a port number.

Additionally, do remember to escape all literal periods in patterns.

kk4uga

8:54 pm on Jan 26, 2011 (gmt 0)

10+ Year Member



"The latest code will not redirect www.example.com:80 requests with a port number."

Do you have a fix? I don't know how to code that.

jdMorgan

9:17 pm on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Already posted above:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$

Jim

kk4uga

3:30 pm on Feb 1, 2011 (gmt 0)

10+ Year Member



Great, thanks!

jdMorgan

7:29 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take the [NC] off your latest RewriteCond. You *do* want to redirect if the case is incorrect.

Jim