Apache server alias wildcard

Something I've learnt recently that I might be the last to know but you can use wildcards (*) in the ServerAlias in Apache. In the past I've done something along the lines of

view plain print about
1<VirtualHost *:80>
2    ServerName example.com
3    ServerAlias www.example.com www.example.co.uk example.co.uk
4</VirtualHost>

Really all you need to do is:

view plain print about
1<VirtualHost *:80>
2    ServerName example.com
3    ServerAlias *.example.*
4</VirtualHost>

Hope this helps someone else

Posted: 18-Aug-2010

View: 4867

Permalink: here

Comments

Good to know, I've always just added additional ServerAlias lines for all my hostnames.

#1 Ryan Stille
18/Aug/10 5:52 PM

Thanks! I've always used the wildcard at the front of the expression, but it never quite worked correctly - eg *.example.com

#2 Richard Mortimer
25/Oct/11 3:22 PM

@richard whats been up then? For local development you still need to set up the domains in the HOST file. So for *.example.com you'd want the following in the HOST urls to test it works

www.example.com
demo.example.com
beta.example.com

#3 Andy Jarrett
25/Oct/11 3:39 PM