Creating Wildcard Sub Domain Using Apache VirtualHost
Asked Answered
V

2

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>
Verret answered 26/11, 2012 at 13:51 Comment(0)
C
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

Cornerstone answered 2/6, 2013 at 0:5 Comment(2)
Link dead as of today.Atalayah
%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.Beniamino
B
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.

Breezy answered 26/11, 2012 at 14:3 Comment(3)
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.comVerret
so I don't think by making it static DocumentRoot will solve this problem. It's dynamic.Verret
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.Breezy

© 2022 - 2024 — McMap. All rights reserved.