Mobile app version of vmapp.org
Login or Join
Samaraweera270

: What is wrong with this Apache httpd.conf file for vhosts? What is wrong here? NameVirtualHost *:80 <VirtualHost *:80> ServerName www.apples.co.uk DocumentRoot /var/www/apples.co.uk RewriteEngine

@Samaraweera270

Posted in: #Apache #HttpdConf #Redirects #Virtualhost

What is wrong here?

NameVirtualHost *:80

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

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


Problems:


apples.co.uk does correctly redirect to apples.co.uk, but
bananas.co.uk redirects to apples.co.uk. When either apples.co.ukor bananas.co.uk redirects, the address
bar results with 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 apples.co.uk (/etc/apache2/httpd.conf:3)
port 80 namevhost apples.co.uk (/etc/apache2/httpd.conf:3)
port 80 namevhost 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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Samaraweera270

1 Comments

Sorted by latest first Latest Oldest Best

 

@Sims2060225

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 about apples.co.uk. So it uses first Virtual Host to serve apples.co.uk. Redirect will work OK here.
Apache does not know about bananas.co.uk -- it only knows about bananas.co.uk. So it uses first Virtual Host to serve bananas.co.uk .. which is apples.co.uk. Therefore it will be redirected to 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 apples.co.uk ServerAlias apples.co.uk
DocumentRoot /var/www/apples.co.uk
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.apples.co.uk [NC]
RewriteRule ^(.*)$ www.apples.co.uk [L,R=301]
</VirtualHost>

<VirtualHost *:80>
ServerName 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 ^(.*)$ www.bananas.co.uk [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme