2

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.

Popcorn245
  • 577
  • 6
  • 14
  • Okay, so you have a different folder for each subdomain? For example, if the user puts on the URL test.example.com, it should go to test.example.com/sites/test/public_html? I think you have misunderstood the configuration, and you should handle another Virtual Host, that will handle each subdomain you want to handle, right? – facundofarias Nov 02 '14 at 14:57
  • @Facu Farias Yes you are correct I have a different folder for each subdomain and i have seen countless people set this up for website hosting, so I know it is possible. The problem with making a new conf for each subdomain is that I have to enter a new DNS entry and conf file for each one. This will also take time to propagate. If I am missing something please correct me. – Popcorn245 Nov 02 '14 at 15:34
  • @FacuFarias and i dont want it to physically go to /sites/{subdomain}/public I want {subdomain}.example.com to have that as its document root. – Popcorn245 Nov 02 '14 at 15:48

2 Answers2

0

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... ^_^

Popcorn245
  • 577
  • 6
  • 14
  • please elaborate the solutions – Muhaimin Jan 02 '15 at 08:41
  • @Mahaimin As I stated, the directory that you are binding the dynamic sud-domains to can't be under a directory that is already bound to a domain. So the solution was to move where I was storing the subdomains to the webroot and then it worked as expected. – Popcorn245 Jan 03 '15 at 17:28
-1

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?

facundofarias
  • 2,973
  • 28
  • 27
  • There are a couple issues with the solution you gave. One, you can't use a wildcard (*) in the Server Name and two, I want each subdomain to map to it's own named folder. As shown with what I have so far this should be attainable using [VirtualDocumentRoot](http://httpd.apache.org/docs/current/mod/mod_vhost_alias.html). Let me know if you need any more details, thanks! ^_^ – Popcorn245 Nov 02 '14 at 16:47
  • Yes, sorry, I didn't test it, it was just an example. What happens if the user put something on the URL that you don't handle? I mean.. You can't map all the possibilities to different sites, isn't? – facundofarias Nov 03 '14 at 14:03