Link to home
Start Free TrialLog in
Avatar of zefon
zefonFlag for United States of America

asked on

redirect mydomain.com to www.mydomain.com

I would like to know how to do the following:

redirect mydomain.com to www.mydomain.com

Currently when a user types in mydomain.com the can browse the website just the same as using www.mydomain.com. This creates a problem on SSL pages because the cert is for www.mydomain.com and when users access the site using mydomain.com they get a warning that the site name does not match the cert. How can I automatically redirect anyone who types in mydomain.com to www.mydomain.com.

Keep in mind that the solution needs to be global because there are search engine listings for links such as mydomain.com/directory/file.html and I would want these automatically change to www.mydomain.com/directory/file.html. Manually entering each file in an htaccess file is not an option.
ASKER CERTIFIED SOLUTION
Avatar of humeniuk
humeniuk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zefon

ASKER

A big thank you Humeniuk. Works like a charm. Now I just have a couple of things to clear up:

I have other domain names pointing to this site and I took your code and it worked on them too. Do I need to use a separate RewriteCond and RewriteRule for each or can I combine it all it one?

Example:

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

or this

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

Do I need the RewriteEngine on for each instance or only once?

Also, for SSL would this work?

RewriteEngine on
RewriteCond %{HTTPS_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=permanent,L]


Yes, this would also work for SSL.

"Do I need to use a separate RewriteCond and RewriteRule for each or can I combine it all it one?"
Take a look at the syntax in the SitePoint link above.  It shows how you can use it for a single site or for all sites on the server depending on where you place it.