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 am using Apache, and would like to redirect all subdomain and non www addresses to the www version using Virtual Hosts.

The only exception to this rule is that I want media.domain.com to point to the same location as www.domain.com because I want a different, cookieless domain for images and video to be located on.. (speed).

The code below is not however working. How would one go about achieving this?

<VirtualHost *:80>
        ServerName www.domain.com
        ServerAlias www.domain.com media.domain.com
        ServerAdmin tom@domain.com
        DocumentRoot /srv/www/domain.com
</VirtualHost>



<VirtualHost *:80>
    ServerName domain.com
ServerAlias domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>
share|improve this question
    
Can you define "not however working"? –  ceejayoz Mar 4 '13 at 18:38
    
media.domain.com redirects to www.domain.com which is not desired.. –  Thomas Clowes Mar 5 '13 at 11:10
add comment

1 Answer

There are a couple of ways of doing this depending on if this is the only domain on the server or not.

You can use a wildcard in the ServerAlias to do a catch all on a domain like below

<VirtualHost *:80>
        ServerName www.domain.com
        ServerAlias www.domain.com media.domain.com
        ServerAdmin tom@domain.com
        DocumentRoot /srv/www/domain.com
</VirtualHost>



<VirtualHost *:80>
    ServerName domain.com
ServerAlias *.domain.com domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>

If this is the only domain on the server then you can use a default vhost

<VirtualHost *:80>
        ServerName www.domain.com
        ServerAlias www.domain.com media.domain.com
        ServerAdmin tom@domain.com
        DocumentRoot /srv/www/domain.com
</VirtualHost>



<VirtualHost _default_:80>
    ServerName domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>

in both cases the default or wildcard will match unless there is a more specific vhost.


In response to the comment can you try.

<VirtualHost *:80>
        ServerName www.domain.com
        ServerAlias www.domain.com media.domain.com
        ServerAdmin tom@domain.com
        DocumentRoot /srv/www/domain.com
</VirtualHost>

<VirtualHost *:80>
        ServerName media.domain.com
        ServerAlias media.domain.com
        ServerAdmin tom@domain.com
        DocumentRoot /srv/www/domain.com
</VirtualHost>    

<VirtualHost *:80>
    ServerName domain.com
ServerAlias *.domain.com domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>
share|improve this answer
    
Thanks for the response. I do have other domains on the server so I have utilized your first example. media.domain.com however simply redirects to www.domain.com Thanks –  Thomas Clowes Mar 5 '13 at 11:10
    
Maybe media needs its own vhost, it can point to the same DocumentRoot, I have updated the answer –  Epaphus Mar 5 '13 at 14:45
    
That still redirects media.domain.com to www.domain.com .. –  Thomas Clowes Mar 5 '13 at 16:55
add comment

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.