Redirect non www to www in htaccess

Ive been looking at where a client’s website is ranking in the search engine. For some reason Google seems to be indexing strange subdomains of their main domain. For example: and I have no idea where these subdomains have come from! Can anyone show me a line that I can put in my htaccess file that redirect anything that isnt ‘www.’ to the ‘www.’ version? I have a redirect from ‘’ redirecting to ‘’, but now I now one which redirect anything that isnt ‘www.’.

[google]all subdomains to www mod_Rewrite[/google] <-this search turns up some solutions.

If this site is your Google Webmaster tools you can suggest which is the main domain, eg www which will deal with your main in a better fashion too, ie before the request hits your server.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\\.yourdomain\\.com [NC]
RewriteRule .?{REQUEST_URI} [L,R=301]


You can use Cpanel for redirecting this one.

Thanks guys.
ScallioXTX: That worked a treat. Worth putting that in every site’s htaccess.

Does a 301 redirect tell the search engines that - not only to redirect the page because it doesnt exist = but also to index the redirected page?

It tells search engines that the requested URL has been permanently moved, so they can index the new page instead of the old/incorrect one (if they have the old/incorrect one indexed)

Will this also redirect to [noparse][/noparse]

if not how, do you have some advice on how to do that also?

Yes, it will :slight_smile: