[HOW-TO] Wildcard subdomains to wildcard directory w/o .htaccess

redesb

Verified User
Joined
May 10, 2004
Messages
193
Location
Spain
Tested on DA 1.38.3 with Apache 2.2

What you get

Request of 'httpd://subdomain.domain.tld' or 'httpd://subdomain.domain.tld/index.html' are served from '/home/user/domains/domain.tld/public_html/directory/subdomain/' or '/home/user/domains/domain.tld/public_html/directory/subdomain/index.html' without change the URL typed on the browser.

How to
  1. Setup the dns with a wildcard A record (*.domain.tld). See [KB] Wildcard *.domain.com

  2. Create a custom VirtualHost template. See [KB] Using a custom VirtualHost template
    Note: I only copied virtual_host2.conf to custom directory but if you use SSL, need to copy/modify also virtual_host2_secure.conf

  3. Modify the copied template(s) moving the '|CUSTOM|' token after 'ErrorLog...' and before '<Directory...' directives.

  4. Login to panel and go to Admin Level -> Custom Httpd configurations and select the domain to change.

  5. Add the following directives in the up panel:

    ServerAlias *.|DOMAIN|
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.tld
    RewriteRule ^/(.*)$ |DOCROOT|/directory/%1/$1 [L]

Save the changes and wait apache restart or go to Admin Level -> Service Monitor and restart it manually.

NOTE:
I use/create one directory bellow 'public_html' (like 'web_of') to store the 'subdomains' but you can skip it and store your 'subdomains' directly bellow 'public_html', just remember that you must modify the 'RewriteRule' directive.

Explanation of the 'RewriteCond' and 'RewriteRule' directives

The first 'RewriteCond' exclude apache requests to 'www.domain.tld'.

The second 'RewriteCond' set the '%1' apache variable to typed value of subdomain. ['([^.]+)']

And finally the 'RewriteRule' directive set the destination directory of the subdomain. In this case to '/home/user/domains/domain.tld/public_html/directory/subdomain/rest_of_the_URI'. [%1 = subdomain, $1 = rest_of_the_URI (index.html, index.php,...)]

How to exclude some subdomain(s)

To exclude some subdomain add one (or more) 'RewriteCond' rule after the first one, like this:
RewriteCond %{HTTP_HOST} !^subdomain_to_exclude\.

Hope this help,
Ramon
 
Back
Top