Mobile app version of vmapp.org
Login or Join
Martha676

: 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

@Martha676

Posted in: #Apache #Subdomain #Virtualhost

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>

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Martha676

2 Comments

Sorted by latest first Latest Oldest Best

 

@Nimeshi995

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);

10% popularity Vote Up Vote Down


 

@Pierce454

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme