Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Config with Wildcard/Non-Wildcard Subdomain Mix

I have the domain, we'll call it "mydomain.com" and I want the following virtual hosts set up to resolve in the following way:

  • mydomain.com / www.mydomain.com to point to /var/www/
  • dev.mydomain.com to point to /var/www/dev/
  • *.mydomain.com (all other subdomains) to point to /var/www/old

My apache configuration is currently set up as:

NameVirtualHost 1.2.3.4:80

<VirtualHost 1.2.3.4:80>
 ServerAlias *.mydomain.com
 DocumentRoot /var/www/old
</VirtualHost>

<VirtualHost 1.2.3.4:80>
 ServerName mydomain.com
 ServerAlias www.mydomain.com
 DocumentRoot /var/www
</VirtualHost>

<VirtualHost 1.2.3.4:80>
 ServerAlias dev.mydomain.com
 DocumentRoot /var/www/dev
</VirtualHost>

Unfortunately, this is not working as I expected. With this configuration, only the first (wildcard) VirtualHost entry works properly.

What is the right way to configure Apache to do this?

like image 560
SoupNutsy Avatar asked Jun 03 '10 19:06

SoupNutsy


1 Answers

Place the wildcard entry last in the file. Apache will use the first VirtualHost that matches the Host header being sent by the browser.

like image 138
ceejayoz Avatar answered Sep 28 '22 00:09

ceejayoz