Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a website which can be reached through www.example.com. Now I want to make it also accessible under example.com. How can I make this?

I have several subdomains e.g. test.example.com, test2.example.com etc. I also read a bit and found out that one should use htaccess to permanently redirect from example.com to www.example.com.

RewriteCond %{http_host} ^example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]

So now there should no duplicate content and it should be SEO friendly. But how do I have to create the A-record? Now there is only one with www.example.com. If I create one with example.com it will automatically mapped to example.com.example.com ...

Then I have to add an entry in the host-file. Does this look like this?

192.168.1.1 example.com

Than I have some entries for the VirtualHost for apache. Is this file correct?

<VirtualHost 192.168.1.1:80>
 ServerName www.example.com
ServerAlias *.www.example.com
ServerAlias example.com

ServerAdmin webmaster@example.com

 DocumentRoot /var/www/html/path

 <Directory /var/www/html/path>
  AllowOverride None
  Options +FollowSymLinks -Indexes -Includes +Multiviews +ExecCGI
  Order allow,deny
  Allow from all
  AllowOverride All
 </Directory>
</VirtualHost>

Solution:

<VirtualHost 192.168.1.1:80>
    ServerName example.com
    Redirect 301 / http://www.example.com/
</VirtualHost>

<VirtualHost 192.168.1.1:80>
    ServerName www.example.com
    ServerAlias *.www.example.com

    ServerAdmin webmaster@example.com

    DocumentRoot /var/www/html/path

    <Directory /var/www/html/path>
        AllowOverride None
        Options +FollowSymLinks -Indexes -Includes +Multiviews +ExecCGI
        Order allow,deny
        Allow from all
        AllowOverride All
    </Directory>
</VirtualHost>

I made this together with two A-records (one for www.example.com and example.com) pointing to the IP-Adress where the webserver resides. Additionally, I had the following entry in /etc/hosts

192.168.1.1 www.example.com example.com

I also state my whitelist-entry from squid:

.example.com
share|improve this question
    
you should have just let the moderation system move the post rather than delete it and resubmit yourself. –  Alnitak Nov 10 '11 at 8:55

2 Answers 2

up vote 3 down vote accepted

But how do I have to create the A-record? Now there is only one with www.example.com. If I create one with example.com it will automatically mapped to example.com.example.com ...

Why example.com.example.com? Just get your DNS host to put in a A record for @ (the domain itself) pointing at the server's IP address.

Then I have to add an entry in the host-file

Not if it's in the DNS, no, you don't.

Than I have some entries for the VirtualHost for apache. Is this file correct?

My personal preference would be to put the "bare" domain name in its own section, with a trivial Redirect permanent directive to push users to the www site.

This will avoid the (admittedly modest) performance hit of having to parse .htaccess files with conditional redirects in them.

It also means that you'll get separate log files for the two names. IMHO this is a good thing - it'll make it easier to tell which domain users are actually arriving from, and avoid the double entry that you'd get in the log if it was done within one virtual server.

share|improve this answer
    
I figured it out why example.com.example.com. I forgot the point at the end. But now I got a warning that already an entry exists (but its not, its one A-record for example.com and one A-record for www.example.com). The system is build so that every domain, needs an entry in /etc/hosts. For the Redirect permanent I edited my question. Is this what you meant? –  testing Nov 10 '11 at 9:13
    
@testing yes, that's what I meant for the Apache config file. –  Alnitak Nov 10 '11 at 10:17
    
I think there is an error in my VirtualHost Configuration. Should I use ServerName example.com instead of ServerAlias example.com? See my edited question. –  testing Nov 10 '11 at 10:47
1  
@testing yes, you should use ServerName and remove the ServerAlias entry because it is also (incorrect) matching www.example.com. –  Alnitak Nov 10 '11 at 12:46
    
Seems this is working! –  testing Nov 10 '11 at 13:37

As for dns entry you should have do it like this:

example.com.        A      192.168.x.y
www.example.com.    CNAME  example.com.

If you have only host file for testing so should do like this:

192.168.1.1 example.com 
192.168.1.1 www.example.com

In that case virtual host file should look like this:

<VirtualHost 192.168.1.1:80>
ServerName example.com
ServerAlias *.example.com

ServerAdmin webmaster@example.com

DocumentRoot /var/www/html/path

<Directory /var/www/html/path>
    AllowOverride None
    Options +FollowSymLinks -Indexes -Includes +Multiviews +ExecCGI
    Order allow,deny
    Allow from all
    AllowOverride All
</Directory>

htaccess file should not be used

share|improve this answer
    
So if the user inputs www.example.com he would be redirected to example.com according to your settings? –  testing Nov 10 '11 at 9:16
    
Yes ,all request containing example.com in name will be redirected to example.com documents. –  Alan Kuras Nov 10 '11 at 9:26
    
it's far better IMHO to have an HTTP redirect from the bare domain to the www address, than to have the www address be a CNAME for the bare name. –  Alnitak Nov 10 '11 at 10:18
    
No its not. Htacces is increasing system load, CNAME is just one position in DNS server which doesnt make any difference. In production environment virtual aliases are handling in that way. Imagin 1000 domain names that need to be checked and forwarded. Isnt better to do this in native way by Apache ? –  Alan Kuras Nov 10 '11 at 11:17
    
I think you are missunderstanding something. Alnitak meant the redirection from example.com to www.example.com is better. Both have the same opinion that htaccess is not a good thing. –  testing Nov 10 '11 at 11:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.