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.





I follow the guideline at ~~[" target="_blank">]( … ntu-jaunty">]( and have created a file of /etc/apache2/sites-available/ with the following content:




DocumentRoot /srv/www/

ErrorLog /srv/www/

CustomLog /srv/www/ combined

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

However, only can access the expected web server. After some trial, I observe the will access the content at /var/www/ instead of /srv/www/ 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" which creaks a symbolic link in sites-enabled?

Look in the default file in sites-available. That might be intercepting the 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 in sites-enabled directory.

I look at default file. The document root is point to /var/www where is exactly where 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 , i get the right index page

But if i go to , i get the apache default page - "it works"

Could you suggest where the problem might be?



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 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 show up. You can have more entries, for example, to your mail - or other subdomains.

In the following example both and will resolve to the same ip address - while will resolve to another ip address.

[blank] Default Edit Remove

www Default Edit Remove

mail 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:

; [60983]

$TTL 86400

@ IN SOA (







@ NS

@ NS

@ NS

@ NS








www A

mail A

@ A

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 would serve from /srv/www/

but would serve from /var/www/

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

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

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

Maybe this will help?


Please enter an answer

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] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct