0

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 [email protected]
        DocumentRoot /srv/www/domain.com
</VirtualHost>



<VirtualHost *:80>
    ServerName domain.com
ServerAlias domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>
2
  • Can you define "not however working"?
    – ceejayoz
    Mar 4, 2013 at 18:38
  • media.domain.com redirects to www.domain.com which is not desired.. Mar 5, 2013 at 11:10

1 Answer 1

1

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 [email protected]
        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 [email protected]
        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 [email protected]
        DocumentRoot /srv/www/domain.com
</VirtualHost>

<VirtualHost *:80>
        ServerName media.domain.com
        ServerAlias media.domain.com
        ServerAdmin [email protected]
        DocumentRoot /srv/www/domain.com
</VirtualHost>    

<VirtualHost *:80>
    ServerName domain.com
ServerAlias *.domain.com domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>
3
  • 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 Mar 5, 2013 at 11:10
  • Maybe media needs its own vhost, it can point to the same DocumentRoot, I have updated the answer
    – Epaphus
    Mar 5, 2013 at 14:45
  • That still redirects media.domain.com to www.domain.com .. Mar 5, 2013 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .