1

I've searched the forums, but can't seem to find a working solution.

My main site is https www.domain.com

I would like the following domains to be redirected to it as well:

http www.domain.com http domain.com https domain.com

Added from comments:

Ok, what I have now is the following in vhost.conf for apache:

<VirtualHost :80> 
ServerName www.domain.com 
ServerAlias www.domain.com domain.com 
</VirtualHost> 

In my .htaccess I have the following:

RewriteCond %{HTTP_HOST} ^domain\.com 
RewriteRule ^(.)$ domain.com/$1 [R=permanent,L] 

Everything works except domain.com which gives a not found error. Just to clarify the SSL is hosted by a CDN hence on the server so apache locally runs with port 80 only. – James 1 hour ago

Jenny D
  • 27,780
  • 21
  • 75
  • 114
James
  • 21
  • 1
  • 2
  • So http://www.domain.com and http://domain.com both work, but https://domain.com and https://www.domain.com do NOT work. I don't see any reference to SSL in your apache config, and you said you are using port 80 only. I don't see how this could possibly work, please elaborate. – David Houde Apr 11 '13 at 11:08

2 Answers2

3

New answer based on information from comments:

First, there's one error in your vhost.conf. You don't need to have www.domain.com both in ServerName and ServerAlias. The alias lists all the other names used for the virtual hosts other than the ServerName. So those lines should be:

ServerName www.domain.com 
ServerAlias domain.com 

And since you have both hostnames in your virtualhost, there's no need for a rewrite, unless you particularly want the URL in the browser's address bar to change to www.domain.com instead of domain.com.


Original answer below

You need to do the following:

  • set up DNS records for domain.com to point to the same place as www.domain.com
  • set up your webserver to respond on port 80 as well as port 443
  • set up your webserver so that domain.com is treated the same as www.domain.com.

For further assistance with just how to do this, show us what you've done so far and ask specific questions.

Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • Ok, what I have now is the following in vhost.conf for apache: ServerName www.domain.com ServerAlias www.domain.com domain.com In my .htaccess I have the following: RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule ^(.*)$ https://www.domain.com/$1 [R=permanent,L] Everything works except https://domain.com which gives a not found error. Just to clarify the SSL is hosted by a CDN hence on the server so apache locally runs with port 80 only. – James Apr 11 '13 at 09:27
  • what I meant is that everything works except https domain.com, which gives a not found error. – James Apr 11 '13 at 09:33
  • OK. I've moved the config info into the question instead, it's really hard to read it in the comment. I've also updated my answer with a basic config fix. But what do you mean with "not found error", do you mean "server not found" or "page not found"? – Jenny D Apr 11 '13 at 11:07
2

The easiest and best method for search engine compliance would be the following .htaccess:

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

This will redirect domain.com to www.domain.com.

To enable https, you will need to configure apache for SSL

David Houde
  • 3,200
  • 1
  • 16
  • 19
  • 1
    I confirm, this answer works. Just put it in the root directory of your server. Now, probably someone might suggest a slightly better solution which would allow both domain.com and www.domain.com to point to /folder. – Placeholder Dec 12 '14 at 08:50