Redirect non www to www in htaccess

Hi.
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: http:mail1.mydomain.com/page1.php and http:demo.mydomain.com/page2.php. 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 ‘http://mydomain.com’ redirecting to ‘http://www.mydomain.com’, but now I now one which redirect anything that isnt ‘www.’.
Thanks

[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 .? http://www.yourdomain.com%{REQUEST_URI} [L,R=301]

:slight_smile:

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 mydomain.com/something.html to [noparse]www.mydomain.com/something.html[/noparse]

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

Yes, it will :slight_smile: