How to setup web server accessed both with and without www?

I follow the guideline of web server setup in Linode Library and successfully setup a web server on Ubuntu 9.04. However, it can only work with www prefix. How can I setup the web server so that it can also accessed without www?

13 Replies

If you are using Apache, you setup a ServerName with the main domain name for that site and a ServerAlias for each additional domain name you want for that site. You can have only one ServerName but many ServerAlias. As long as there is a DNS entry for these pointing to your IP, you will be good to go.

ServerName www.example.com

ServerAlias example.com

ServerAlias www.example.net

ServerAlias example.net

I follow the guideline at ~~[http://library.linode.com/web-servers/apache/installing-apache-ubuntu-jaunty" target="_blank">](http://library.linode.com/web-servers/a … ntu-jaunty">http://library.linode.com/web-servers/apache/installing-apache-ubuntu-jaunty]( and have created a file of /etc/apache2/sites-available/example.com with the following content:

ServerAdmin [email protected]

ServerName example.com

ServerAlias www.example.com

DocumentRoot /srv/www/example.com/public_html/

ErrorLog /srv/www/example.com/logs/error.log

CustomLog /srv/www/example.com/logs/access.log combined

DNS have both example.com and www.example.com A/AAAA records point to my IP.

However, only www.example.com can access the expected web server. After some trial, I observe the example.com will access the content at /var/www/ instead of /srv/www/example.com/public_html/. Any idea?

Well you have it setup exactly like I do. So where could the problem be.

You might already have, but if not, try these:

Did you enable the site by running "sudo a2ensite example.com" which creaks a symbolic link in sites-enabled?

Look in the default file in sites-available. That might be intercepting the example.com requests. Try to change the document root in the default and reloading apache, "sudo /etc/init.d/apache reload"

That is all I can think of right now.

yes, I have run a2ensite. I saw there is a sym-link of example.com in sites-enabled directory.

I look at default file. The document root is point to /var/www where is exactly where example.com goes now. However, I will have more than one domain on this server soon. The change on default file seems not a solution.

Thanks for your kind reply. I think there is something still not yet set correctly but I don't know where. What a headache…

I was suggesting to change the DocumentRoot in default to test that, that is the config that is intercepting your non www site. IF you find it is so, then change it back and then you know where to proceed.

I have about 20 domains on my Linode, but one of them is my primary domain name. I have that setup in the default file. That way if something is misconfiguration it points to the primary website. A misconfiguration is there is a DNS entry to my IP and there is no VirtualHost to catch the domain, it will default o the default site instead of loading the "It Works" page.

I have configured apache for virtual host.

When i go to www.mysite.com , i get the right index page

But if i go to http://mysite.com , i get the apache default page - "it works"

Could you suggest where the problem might be?

Thanks

Abhishek

Verify your ServerName and ServerAlias settings.

Are your DNS Record settings correct?

Not to be simple, but sometimes it is the simple things which get overlooked, so,…

Log in to your Linode account and go to Linode Manager

go to the DNS Manager tab.

Scroll down and look at A/AAAA Records

You will see fields for : Host Name IP Address TTL Edit Remove

The first row of entries has nothing in the first field - this makes your site http://example.com show up - and it should have your ip address in the second field of that row.

The second row should have www in the first field, and your ip address in the second field. This makes http://www.example.com show up. You can have more entries, for example, to your mail - or other subdomains.

In the following example both http://example.com and http://www.example.com will resolve to the same ip address - while http://mail.example.com will resolve to another ip address.

[blank] 12.107.140.123 Default Edit Remove

www 12.107.140.123 Default Edit Remove

mail 12.107.141.456 Default Edit Remove

@Kjb thanks for the detailed reply and guidance.

My A/AAAA Records are set up exactly the way you described it.

But still having the same problem.

@BarkerJr: My ServerName & ServerAlias are correct but still having the problem.

Any other suggestions?

I'm having the exact same problem. Has anyone found the solution for this yet?

I'm having the same exact problems…

This is particularly frustrating because it seems like everything is setup correctly. Here is my rendered DNS file from DNS Manager:

; underbellygames.com [60983]

$TTL 86400

@ IN SOA ns1.linode.com. 69e67145e8ee472d9ccd4fecb3dfbc61.protect.whoisguard.com. (

2009112308

7200

7200

1209600

86400

)

@ NS ns1.linode.com.

@ NS ns2.linode.com.

@ NS ns3.linode.com.

@ NS ns4.linode.com.

@ MX 20 ALT1.ASPMX.L.GOOGLE.COM.

@ MX 10 ASPMX.L.GOOGLE.COM.

@ MX 20 ALT2.ASPMX.L.GOOGLE.COM.

@ MX 30 ASPMX2.GOOGLEMAIL.COM.

@ MX 30 ASPMX3.GOOGLEMAIL.COM.

@ MX 30 ASPMX4.GOOGLEMAIL.COM.

@ MX 30 ASPMX5.GOOGLEMAIL.COM.

www A 74.207.252.118

mail A 74.207.252.118

@ A 74.207.252.118

I'm not sure if the order matters? I don't see any way to change the order.

Any ideas? Thanks!

No, order doesn't generally matter.

But with a positive TTL of 86,400 seconds (the $TTL 86400 line), changes will take up to 24 hours to fully propagate. A smaller TTL (e.g. 3600) is recommended but not critical for this issue.

I recently signed up here, somewhat of a linux newb, and had the same problem. I followed the tutorial in the wiki concerning Virtual Hosts and found that

www.example.com would serve from /srv/www/example.com/public_html/

but

example.com would serve from /var/www/

My problem was with /etc/hosts. I had:
> 127.0.0.1 localhost.localdomain localhost hostname

123.45.67.89 example.com hostname

I removed the "example.com" domain from the second line, e.g.
> 127.0.0.1 localhost.localdomain localhost hostname

123.45.67.89 hostname

and now Apache serves both example.com and www.example.com correctly, i.e. from /srv/www/example.com/public_html

Maybe this will help?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct