Results 1 to 3 of 3

Thread: Apache: Redirecting multiple domains from non-www to www

  1. #1
    Bronze Member
    Join Date
    Dec 2002
    Location
    Derbyshire, England
    Posts
    104

    Question Apache: Redirecting multiple domains from non-www to www

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

    What I'm trying to do is cope with the situation where the 'www.' is missing, i.e. onedomain.com, is to use www.onedomain.com, using the httpd.conf file. 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.

    It seems to me that something like the following ought to be what I want but I've never dabbled in this rewrite stuff before and could well be hopelessly off target:

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

    I hope someone can point me in the right direction because, without a single method of doing it for all hosted domains, I'm going to have to add a lot of extra VirtualHost stuff to the conf file!
    Ken

    To err is human but to really foul things up takes a computer!

  2. #2
    Nobody knows I'm a dog. TZ Veteran petard's Avatar
    Join Date
    Feb 2003
    Location
    Newspapastan
    Posts
    1,050
    You might be better off adding a 'ServerAlias' directive to each of the VirtulHosts areas. I'm sure it's a pain in the rear but it's a good practice to keep all of the specifics configs of a site in it's own configuration area (<Virtual Host>) and minimalize the Global Directives.

    Many thanks to egghead for the cool .sig

  3. #3
    Bronze Member
    Join Date
    Dec 2002
    Location
    Derbyshire, England
    Posts
    104
    Well that could be done but what would be the effect on search engines?

    For each hosted domain, there are several domains with permanent redirects to the main one, eg for www.domain.com there might be

    <VirtualServer *>
    ServerName www.domain.com
    DocumentRoot .....
    ServerAlias domain.com
    </VirtualServer>

    <VirtualServer *>
    ServerName www.domain.co.uk
    ServerAlias domain.co.uk
    Redirect 301 www.domain.com
    </VirtualServer>

    OK, I've got domain.co.uk and www.domain.co.uk permanently mapping to the main URL but what about the main alias, domain.com?

    Or maybe the way is to handle domain.com in another VirtualServer?

    Whilst I take your point that generally it is better to keep each VirtualServer with all its stuff, it is a lot of extra work in the first instance and it is a totally generic redirection that I'm after; anydomain -> www.anydomain.
    Ken

    To err is human but to really foul things up takes a computer!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •