17

I want to have this situation :

  1. if user request using this URL : example.com or www.example.com, user will see index.php in this directory /home/admin1/public_html/

  2. but when user request using other sub domain (wildcard) for example : freediscount.example.com, user will see index.php in this path : /home/admin1/public_html/userweb/freediscount.example.com

technical support on my hosting suggest me to use this method : http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html

based on that tutorial, the PHP has a new job... to redirect on specific folder when user request with sub domain. I don't like this method. for me, it would be better if Apache can handle this.

nearly close to what I need is this method : Virtualhost For Wildcard Subdomain and Static Subdomain

but, I have a problem with VirtualHost setting, how to create VirtualHost correctly for that situation?

here's what I've done but didn't work :

## I think this one is for www or without www, automatically generated with WHM
<VirtualHost xx.xx.xx.xx:80> 
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/admin1/public_html
</VirtualHost>

## Here's what I'm trying to add
<VirtualHost xx.xx.xx.xx:80>
    ServerName example.com
    DocumentRoot /home/admin1/public_html/userweb/*
</VirtualHost>
Community
  • 1
  • 1
Saint Robson
  • 5,475
  • 18
  • 71
  • 118

2 Answers2

15

Wildcard sub-domains are definitely possible using Apache virtual hosts.

I had basically the same requirements and managed to get it working with Apache's mod_vhost_alias.so module. Try this in your http-vhosts.conf file:

DocumentRoot "/home/admin1/public_html/userweb/" 
<Directory "/home/admin1/public_html/userweb/"> 
    Options None 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
</Directory>

<VirtualHost *:80>
    DocumentRoot /home/admin1/public_html/
    ServerName www.example.com
</VirtualHost>

<VirtualHost *:80> 
    VirtualDocumentRoot /home/admin1/public_html/userweb/%1.example.com/ 
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /home/admin1/public_html/
    ServerName example.com
</VirtualHost>

Note that I haven't tested this, but it's pretty close to the solution that worked for me.

Full details of my solution are here: http://www.calcatraz.com/blog/wildcard-subdomains-in-apache-1422

Dan
  • 614
  • 6
  • 7
9

Try with this:

NameVirtualHost *:80

<VirtualHost *:80>
  DocumentRoot /home/admin1/public_html/
  ServerName www.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /home/admin1/public_html/userweb/freediscount.example.com
  ServerName  other.example.com
  ServerAlias *.example.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /home/admin1/public_html/
  ServerName example.com
</VirtualHost>

Order of virtual hosts & their specificity matters.

tonino.j
  • 3,837
  • 28
  • 27
  • freediscount.example.com is just an example. in a real world, user can type subdomain they created. for example : mystore.example.com >> /home/admin1/public_html/userweb/mystore.example.com – Saint Robson Nov 26 '12 at 14:11
  • so I don't think by making it static DocumentRoot will solve this problem. It's dynamic. – Saint Robson Nov 26 '12 at 14:12
  • You can't make wildcard DocumentRoots. You need to do that thru .htaccess file or thru php. This is what you can do with virtualhosts as is. MAybe there is apache mod to take care of what you want. But as is, this is it I think. – tonino.j Nov 29 '12 at 17:05