Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 900
  • Last Modified:

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.
0
zefon
Asked:
zefon
  • 2
1 Solution
 
humeniukCommented:
"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.
0
 
zefonAuthor Commented:
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]


0
 
humeniukCommented:
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now