Forum Moderators: phranque

Message Too Old, No Replies

301 redirect for non-www to www

301 redirect for non-www to www (which code is best)

         

riospace

12:17 am on Oct 14, 2006 (gmt 0)

10+ Year Member



Which one, if any, of the following would be the best .htaccess code for a permanent redirect from non-www to www?

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

or this:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ [domain.com...] [R=301,L]

Also, what is the Options +FollowSymLinks line for? I have noticed that many people do not put that line in their redirects. What do you recommend for a simple redirect for non-www to www. Is it even a good idea to have this kind of redirect?

riospace

3:51 am on Oct 14, 2006 (gmt 0)

10+ Year Member



Do I need to add the RewriteBase / code above? How do I know if I need the RewriteBase / code for a non-www to www redirect?

riospace

11:34 am on Oct 14, 2006 (gmt 0)

10+ Year Member



I know that my question is probably annoying since it is talked about so much in the forums, but that is why I am asking it.

There are so many versions of 301's in the forums, that I narrowed it down to two that I think are good for my needs.

I just wanted to get these verified because the details are confussing and I know that one little mistake will put my site into a tail spin.

Just a little paranoid and would like some verification.

Also, I did figure out the Options +FollowSymLinks

LunaC

1:12 pm on Oct 14, 2006 (gmt 0)

10+ Year Member



The difference I can see between example 1 and 2 is that 2 also anchors the end so it will also cover if someone types in something like example.com./ (notice the dot before the slash) and redirect it in one smooth motion to www.example.com/ the first one will redirect to www.example.com./ (not what you want probably).

I recently had someone link to me with the dot before the slash and since I changed the code to the second example and the search engines seem no longer to be having a problem, they follow the 301 and continue spidering.

If you add anything to your .htaccess be sure to clear your cache and check headers, you'll want to see a clean 301 redirect. The best way I found of checking is using the Firefox extension Live HTTP Headers.

(I'm still very much at the beginning learning stages, so if anyone else can jump in and correct if I'm wrong, please do.)