Creating Wildcard Sub Domain Using Apache VirtualHost

41,973

Solution 1

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

Solution 2

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.

Share:
41,973

Related videos on Youtube

Saint Robson
Author by

Saint Robson

Updated on June 02, 2020

Comments

  • Saint Robson
    Saint Robson about 4 years

    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>
    
  • Saint Robson
    Saint Robson over 11 years
    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
    Saint Robson over 11 years
    so I don't think by making it static DocumentRoot will solve this problem. It's dynamic.
  • tonino.j
    tonino.j over 11 years
    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.
  • kontur
    kontur over 6 years
    Link dead as of today.
  • maliayas
    maliayas about 6 years
    %1.example.com will fail (. is a metacharacter and must be followed by an integer in this case). It can be %1.0.example.com as a workaround.