301 redirect help please

I am about to start designing my new portfolio site and I am using Google’s WMT. I wanted to know how to sort out the preferred domain issue and the pros/cons of using the www part or doing without it.

This thread I found helped a lot but I don’t know anything about redirects. So I’ve followed the instructions in this thread (apart from the .htaccess file which I’m about to do). I found the following code in the thread I’ve linked to, butI prefer typing the URL without the www part and have set it accordingly in WMT and I’m not sure if this code will redirect users/crawlers from the www version to the non-www version of my site or if it does it the other way around. Can someone please tell me if I need to alter the code below to redirect users/crawlers from the www version to the non-www version?

[QUOTE=tomovuk;4449888]The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc][/QUOTE]

Thank you very much both of you. :slight_smile:

@ScallioXTX
Just curious. I’ve been digging around in cPanel and I see that I can manage the 301 redirects from www to non-www (and vice versa) from there but if I did that from cPanel, would it make all the search engines do what they guys were talking about in the thread I linked to? Or would it be better for me to use the updated code you’ve just posted?

Excellent! I think that would be an easier way for me to do it because I don’t know anything about writing code for .htaccess. When I was fiddling around with the hotlinking protection in cPanel, after I’d copied and pasted the code you posted in my .htaccess file, cPanel displayed an error to do with the code. So I think I’ll do it through cPanel instead. But thank you for helping me out.

So long as it’s a 301 redirect (not a 302!) you should be fine as far as link juice is concerned :slight_smile:

From www to non-www would be


Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\\.domain\\.com [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [L,R=301]

(also fixed some errors that were in there …)

That code forces all requests to use the ‘www’ version.

I used to have that approach (I thought that the ‘www’ was largely redundant and was swayed by sites like no-www.org), but now, with more knowledge and a desire to use smart cookie-less sub-domains for static resources such as images, CSS and JS, I switched to the ‘www’ variant.

You DO have to be careful with .htaccess, as you can end up doing some spooky stuff that gets hard to trouble-shoot with regex.