Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

What is wrong here?

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.apples.co.uk
DocumentRoot /var/www/apples.co.uk
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.apples\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.apples.co.uk/$1 [L,R=301]
</VirtualHost>

<VirtualHost *:80>
ServerName www.bananas.co.uk
DocumentRoot /var/www/bananas.co.uk
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.bananas\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.bananas.co.uk/$1 [L,R=301]
</VirtualHost>

Problems:

  • apples.co.uk does correctly redirect to www.apples.co.uk, but bananas.co.uk redirects to www.apples.co.uk.

  • When either apples.co.ukor bananas.co.uk redirects, the address bar results with http://www.apples.co.uk//. What is causing that extra slash at the end?

Here the output of apachectl -S :

[Sun Jul 31 08:41:52 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server www.apples.co.uk (/etc/apache2/httpd.conf:3)
         port 80 namevhost www.apples.co.uk (/etc/apache2/httpd.conf:3)
         port 80 namevhost www.bananas.co.uk (/etc/apache2/httpd.conf:11)
         port 80 namevhost servername.apples.co.uk (/etc/apache2/sites-enabled/000-default:1)
Syntax OK

I guess the first problem is caused by the default server setting. How do I remove that? Does there need to be a default?

I know I also need to add to sites-enabled to get rid of the warning, but I'll leave that for another question.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

FIRST:

  1. First defined VirtualHost will be used as catch all for unknown domain names.

  2. Apache does not know about apples.co.uk -- it only knows about www.apples.co.uk. So it uses first Virtual Host to serve apples.co.uk. Redirect will work OK here.

  3. Apache does not know about bananas.co.uk -- it only knows about www.bananas.co.uk. So it uses first Virtual Host to serve bananas.co.uk .. which is www.apples.co.uk. Therefore it will be redirected to www.apples.co.uk.

To solve the above issue: Add ServerAlias apples.co.uk line inside first virtual host and ServerAlias bananas.co.uk into second one. If you want to catch ANY subdomain as well -- add this line as well: ServerAlias *.apples.co.uk (the same for bananas.co.uk).


SECOND:

The RewriteRule URL will include leading slash if declared in server config / virtual host context. If this rewrite rule would be declared in .htaccess it would work fine, but here (inside VirtualHost declaration) you need to remove slash after domain name in RewriteRule target. That is why you are having double slash after domain name on redirect.

You can also speed up host matching a bit by replacing relatively expensive regex by simple string comparison: !^www\.bananas\.co\.uk$ => !=www.bananas.co.uk.


NameVirtualHost *:80

<VirtualHost *:80>
   ServerName www.apples.co.uk
   ServerAlias apples.co.uk
   DocumentRoot /var/www/apples.co.uk
   RewriteEngine On
   RewriteCond %{HTTP_HOST} !=www.apples.co.uk [NC]
   RewriteRule ^(.*)$ http://www.apples.co.uk$1 [L,R=301]
</VirtualHost>

<VirtualHost *:80>
   ServerName www.bananas.co.uk
   ServerAlias bananas.co.uk
   ServerAlias *.bananas.co.uk
   DocumentRoot /var/www/bananas.co.uk
   RewriteEngine On
   RewriteCond %{HTTP_HOST} !=www.bananas.co.uk [NC]
   RewriteRule ^(.*)$ http://www.bananas.co.uk$1 [L,R=301]
</VirtualHost>

I guess the first problem is caused by the default server setting. How do I remove that? Does there need to be a default?

Create VirtualHost for some fake domain name .. and place it first (before any other <VirtualHost> declarations) -- then you will see Apache's error page when such unknown domain name will be requested.

share|improve this answer

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.