Redirecting multiple domains from non-www to www

Discussion in 'Apache' started by trentham, Sep 20, 2006.

  1. #1
    I'm looking after a server which supports multiple domains, though we don't have control of the DNS server.

    Hosted sites typically have several domains which I'm handling with permanent redirects in httpd.conf, eg.

    <VirtualHost *>
    ServerName www.onedomain.co.uk
    Redirect 301 www.onedomain.com
    </VirtualHost>

    That's fine and is provided mainly for a bit of background.

    What I'm now trying to do is cope with the situation where the 'www.' is missing, i.e. onedomain.com, is to use www.onedomain.com. At the moment, the DNS server is sending the requests to the web server but because it doesn't know the domain name, they're just dropping through to the default site.

    Now I realise that I could set up a rewrite rule

    <VirtualHost *>
    ServerName onedomain.com
    RewriteEngine on
    RewriteRule ^/(.*) http://www.onedomain.com/$1 [L,R=301]
    </VirtualHost>

    (though why is that recommended rather than a simple permanent redirect?)

    However is there anyway I can do a single global set of rules to handle all the domains? There are a lot of domains which will need to be added otherwise!
     
    trentham, Sep 20, 2006 IP
  2. trentham

    trentham Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I've been thinking and studying further about this and I suspect that something along the following lines might get there, but I think I need a bit of help to get the syntax correct!

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^/(.*) http://%{HTTP_HOST}/$1 [R=301]


    Is this getting anywhere close or am I totally off track?
     
    trentham, Sep 20, 2006 IP