简体   繁体   中英

Apache 2.4 Wildcard Subdomain Virtual Host

My goal is to have the following occur, when the user visits the sites below to the left it takes them to the directories on the right:

www.example.com or example.com  =>  /var/www/example/public_html/

*.example.com                   => /var/www/example/public_html/sites/%1/public_html/

I have tried the following:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example/public_html
</VirtualHost>

<VirtualHost *:80>
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/example/public_html/sites/%1/public_html
</VirtualHost>

The main domain works with or without www, but every time I try to visit any other subdomain it takes me to and gives me a 500 Internal Server Error. The URL changes to look like below when I type in a subdomain:

{subdomain}.example.com/sites/{subdomain}/public_html/

I am on Apache 2.4.7 and yes I have setup the WildCard on my DNS server. I would greatly appreciate any answers deeming that I have been slamming my head on a wall for 2 weeks trying to get this working.

I think the configuration you are looking for it's quite similar to this one:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example/public_html
</VirtualHost>

<VirtualHost *:80>
  ServerName *.example.com
  ServerAlias *.example.com
  DocumentRoot /var/www/wildcardexample/public_html
</VirtualHost>

What to you think? In this case, any subdomain will be redirected to the same site. Is this the configuration you want? Or you want different configurations for each subdomain?

Okay the whole problem was from the wildcard directory being beneath another directory that is already bound. After I made a directory elsewhere it started working like expected, go figure...

Hope this helps anyone else who happens to run into this odd bug... ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM