How to setup virtual subdomains with just Mod_rewrite and .Htaccess (+500 bonus)
Posted on 2006-11-13
Hello and thank you for taking the time to read this question. Before I begin, as this question is quite challenging i'm awarding an additional 500 bonus points to the person who can provide me a working .Htaccess file/code that achieves the desired result.
Let me begin.
Assume that website mydomain.com is hosted on a dedicated host with a static IP.
A wildcard domain entry has been added so that any subdomains are redirected to the domain name. For example: somesubdomain.mydomain.com/ --> redirects to mydomain.com/
Mydomain.com contains one index.html file in its root and is the default start page for the website.
What I want to achieve:
1. Any requests to subdomains must be redirected to my root domain path, however, the redirect process needs to be transparent to the users browser, thus the user must still see their original url they entered.
For example: user enters subx.mydomain.com, user is redirected transparently to mydomain.com/index.html, user is shown the same url as entered. subx.mydomain.com
Note that certain subdomains need to be excluded, for example www.
2. Should the user enter a non existent resource path, the user will be redirected to mydomain.com/index.html, again, this redirect needs to be transparent and the user shown the exact same url they entered.
For example: user enters www.mydomain.com/somedir1/somdir2/, user is transparently redirected to mydomain.com/index.html but still shown their original url www.mydomain.com/somedir1/somdir2/
Please feel free to ask questions should you require more information.