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

I've tried looking at other similar questions, but can't really find what I'm looking for.

I have a server running apache2, and I want all requests for www.foo.tld to serve the pages in /var/www/foo.tld

For example:

www.example.com  -> /var/www/example.com
example.net      -> /var/www/example.net
foobar.co.uk     -> /var/www/foobar.co.uk
www.foobar.co.uk -> /var/www/foobar.co.uk

I can't figure out how to do this. I can get it working sometimes with or without the www subdomain, but never with both.

share|improve this question

2 Answers 2

The VirtualDocumentRoot apache directive will do the trick, here is a good explanation : http://httpd.apache.org/docs/2.0/vhosts/mass.html

share|improve this answer
    
Ah I see. Yes this would be the way to do it. –  Tom H May 28 '12 at 14:25
    
How do I get this to work both with and without www subdomain? –  Rory May 28 '12 at 15:49
    
@Blazemore I'd say symlink www.foobar.co.uk to foobar.co.uk. –  Shane Madden May 29 '12 at 1:32

I fixed the issue by creating a file /etc/apache2/sites-enabled-000-Dynamic

And renaming the file 000-default to 001-default.

In this file is the following

<VirtualHost *:80>

  RewriteEngine On

  # Redirect www to non-www (class B compliance with no-www)
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1$1 [R=301,L]

  # Remove trailing slashes
  RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

  # Automatically get the correct folder
  UseCanonicalName Off
  VirtualDocumentRoot /var/www/%1

</VirtualHost>

I changed it slightly: the name www.foobar.com now points to /var/www/foobar which makes it a lot easier.

As well as the dynamic virtual hosts, this also redirects www.foobar.com to foobar.com, and remov

share|improve this answer

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.