2

I would like to setup a VirtualHost which allows for wildcard subdomains such as foo.sites.example.com and bar.sites.example.com. I do not, however, wish to allow multiple level subdomains such as foo.bar.sites.example.com. The following meets the first requirement but not the second. It could be accomplished with mod_rewrite, however, I expect there is a simpler way. I am using Apache 2.2 but could upgrade if necessary. How is this accomplished?

<VirtualHost *:80>
    ServerName sites.example.com
    ServerAlias *.sites.example.com
    # ....
</VirtualHost>
2

To restrict the use of foo.bar.sites.example.com you have to place another VirtualHost above the existing one:

<VirtualHost *:80>
    ServerName foo.bar.sites.example.com
    ServerAlias *.*.sites.example.com
    # ....
</VirtualHost>

Now you can block or redirect the access. However it's important that you place it above the other VirtualHost since Apache always takes the first VirtualHost when multiple entries match.

  • 2
    I don't think we can use wildcard with ServerName. – Nikhil Supekar Jan 28 '15 at 13:36
  • @NikhilSupekar Thanks for pointing out, I adjusted my answer. – debite Jan 28 '15 at 14:09
1

Send all request (ServerAlias *.*.example.com) to a php file. And redirect where you want :

$url=$_SERVER['HTTP_HOST'];

$subdomains=explode('.', $url);

$subdomains[0] : first subdomain
$subdomains[0] : second subdomain ...

You can redirect with

header('Location: '.$URL);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.