What is wrong with this Apache httpd.conf file for vhosts?
FIRST:
First defined VirtualHost will be used as catch all for unknown domain names.
Apache does not know about
apples.co.uk
-- it only knows aboutwww.apples.co.uk
. So it uses first Virtual Host to serveapples.co.uk
. Redirect will work OK here.Apache does not know about
bananas.co.uk
-- it only knows aboutwww.bananas.co.uk
. So it uses first Virtual Host to servebananas.co.uk
.. which iswww.apples.co.uk
. Therefore it will be redirected towww.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.
Related videos on Youtube
paradroid
Referral link for second free bonus node at Cloudkick (Linux/Windows server health monitoring with email/SMS alerting)
Updated on September 18, 2022Comments
-
paradroid over 1 year
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 towww.apples.co.uk
, butbananas.co.uk
redirects towww.apples.co.uk
.When either
apples.co.uk
orbananas.co.uk
redirects, the address bar results withhttp://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.