[Ryedale] Multiple Domains - Apache

Paul Teasdale pdt at ryetek.co.uk
Tue Apr 29 13:46:55 BST 2008


Hi Deepan,
>
> However all requests to http://domainb.com/ gets
> redirected to http://www.domaina.com  whereas the
> expected behaviour is to get redirected to
> http://www.domainb.com/ .
>
I don't think there is anything wrong with your RewriteCond and RewriteRule
at all. You have not said in your e-mail but I bet if you put
http://www.domaina.com or http://www.domainb.com into your browser then it
works fine. Putting http://domaina.com does work but I think is actually
just luck because domaina is your first VirtualHost block. Putting
http://domainb.com actually shows domaina.com as you have stated.

I'm no Apache expert but here's what I think is happening.

I think the VirtualHost stuff decides which block to use based on what the
user types into the URL of the browser.

When you type http://domaina.com it cannot match any ServerName (because
you've put www.domaina.com) and so just by luck it picks the first
VirtualHost block which just happens to be www.domaina.com. It therefore
appears to work.

When you type http://domainb.com it cannot match any ServerName so it uses
the incorrect domaina.com block simply because it's the first VirtualHost
block.

I reckon http://www.domaina.com and http://www.domainb.com will work fine
because they match exactly on the ServerName and therefore the correct
VirtualHost block will be used.

What I would do is create another two VirtualHost blocks (in addition to the
existing ones) as such:

        <VirtualHost  *:80>
        DocumentRoot /var/www/html/domaina/
        ServerName domaina.com
        Options +FollowSymLinks
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^domaina\.com$ [NC]
        RewriteRule ^(.*)$ http://www.domaina.com$1 [R=301,L]
        </VirtualHost>

        <VirtualHost *:80>
        DocumentRoot /var/www/html/domainb/
        ServerName  domainb.com
        Options +FollowSymLinks
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^domainb\.com$ [NC]
        RewriteRule ^(.*)$ http://www.domainb.com$1 [R=301,L]
        </VirtualHost>

and you will find that it then all works. Note the only thing I have altered
is the ServerName. It now does not matter if a user puts
http://www.domainb.com or http://domainb.com because there is a VirtualHost
block to handle each case.

Furthermore in the two blocks you already have (not shown above) you could
remove all the Rewrite lines in each block because if the user types
http://www.domaina.com or http://www.domainb.com there is no need to Rewrite
them in any way.

There may be some easier way to do this but I have tried out the above (on a
Windows box) this lunchtime and it does actually work. I have also tried
removing the two above blocks and then I got exactly what you are getting.

Hope this helps,

Regards,
Paul.






More information about the Ryedale mailing list