Print

Printed May 24, 2013 from B&T's Tips & Scripts
http://tips-scripts.com


Domain and subdomain pointing

Stop here!  If you want to do domain pointing, before you even start with htaccess code, make sure your pointed domain name resolves to your main domain name default page.  Many people think they have trouble with their htaccess code only to find the domain name is not properly pointed to their main domain in the first place.  So make sure your pointed domain name works, resolving to your default page, before going any further.  If you are doing subdomain pointing make sure that wildcard subdomains are enabled in your dns.  You can verify this by using any subdomain name and seeing if it resolves to your main domain default page.
The examples below assume that your root public directory (where you would put this .htaccess file) is htdocs, which is the default setup for Apache.   If your setup is different, then you may need to adjust the examples accordingly.

This approach has you code the first part of the domain or subdomain name and the associated subdirectory in your .htaccess file.   For example:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST} ^(www\.)?
name\.
RewriteRule ^(.*)$
subdirectory/$1 [L]

Note the second line has name, NOT name.com.   And note that there are some restrictions when using this method. There is also a simplified variation of this method that can be used in less complex situations where you are simply pointing a domain name and want ALL subdomains of that one domain to be pointed to a specific directory.   In this example name.com and any subdomain (including www) of that domain would be pointed to htdocs/subdirectory:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/
subdirectory/
RewriteCond %{HTTP_HOST}
name.com$
RewriteRule ^(.*)$
subdirectory/$1 [L]

In both examples, the htacess code works this way.   The first two lines set up the Apache server rewrite process.   Then you have two RewriteCond statements and a RewriteRule for EACH domain or subdomain you wish to point.
There is a second approach where pointed domains and subdomains become automatic, meaning no specific code is required for each pointer.   While this method is much easier to use and does not need to be modified when adding more domains or subdomains, note that there are some restrictions when using this method. RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)
RewriteCond %{DOCUMENT_ROOT}/-%2 -d
RewriteRule ^(.*)$ -%2/$1 [L]

The htaccess code works this way.  The first two lines set up the Apache server rewrite process.  The first RewriteCond allows www. to be a prefix and captures the request name.  The second RewriteCond checks for the existence of a subdirectory with the request name.  The third RewriteCond ensures we are not caught in a looping operation.  The RewriteRule rewrites the request with the subdirectory name (which was already verified).
You can get more information on the Apache Module mod_rewrite here.