: Apache wildcard domains - Restrict number of subdomain levels I would like to setup a VirtualHost which allows for wildcard subdomains such as foo.sites.example.com and bar.sites.example.com. I
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>
More posts by @Martha676
2 Comments
Sorted by latest first Latest Oldest Best
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);
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.