Virtual sub domains.
Note: this will only work if you have DNS for the sub domains (probably
Wildcard DNS) and the server configured to send the requests to the same document root.
For example,
abc.example.com and ww
w.abc.example.com to example.com/
abc/,
def.example.com and ww
w.def.example.com to example.com/
def/ etc.
Note: If you have access to the httpd.conf file do not use mod_rewrite, use
mod_vhost_alias.
In a .htaccess file in the document root
- Code: Select all
Options +FollowSymLinks
RewriteEngine On
# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /%2/$1 [QSA,L]
You can rewrite to a file based on the sub domain name. For example,
abc.example.com/* and ww
w.abc.example.com/* to example.com/dir/
abc.ext?request_uri=*,
def.example.com/* and ww
w.def.example.com/* to example.com/dir/
def.ext?request_uri=* etc. In a .htaccess file in the document root
- Code: Select all
Options +FollowSymLinks
RewriteEngine On
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /dir/%2.ext?request_uri=$1 [QSA,L]
You can rewrite to a file and add query string parameters based on the sub domain name. For example,
abc.example.com/* and ww
w.abc.example.com/* to example.com/file.ext?sub_domain=
abc&request_uri=*,
def.example.com/* and ww
w.def.example.com/* to example.com/dir/file.ext?sub_domain=
def&request_uri=* etc. In a .htaccess file in the document root
- Code: Select all
Options +FollowSymLinks
RewriteEngine On
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /file.ext?sub_domain=%2&request_uri=$1 [QSA,L]