redirect mydomain.com to www.mydomain.com

Posted on 2005-02-28
Medium Priority
Last Modified: 2013-12-24
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.
Question by:zefon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 33

Accepted Solution

humeniuk earned 1000 total points
ID: 13421515
"Manually entering each file in an htaccess file is not an option."
I'm not sure if this means you don't have access to the .htaccess file (as in some shared hosting environments) or if it means there are just too many pages to do a manual entry for each.  If the latter is the case, you can put the following rewrite rule in your .htaccess file so that all pages will be rewritten:

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

By making this a permanent redirect, search engines will pass along the benefits of incoming links to the replacement (ie. www.etc . . . ) pages.  Presumably if you are using SSL, you want to change that to https://www.etc . . .   For a little more info on syntax, etc. see www.sitepoint.com/forums/archive/index.php/t-151418.html.

If this is a case where you have no access to the .htaccess file at all, please let us know.  There are ways of doing this with PHP or ASP scripting as well.

Author Comment

ID: 13421774
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?


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]

LVL 33

Expert Comment

ID: 13421801
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.

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had an issue with InstallShield not being able to use Computer Browser service on Windows Server 2012. Here is the solution I found.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question