Getting Started

ID #227

Can I have a catch-all subdomain?

Applies to: Grid System

You can always create any subdomain (anything.yourdomain.com) that you want by creating a directory or symbolic link in your /htdocs directory as described here.

However, if you want a browser to be able to get to your site no matter what subdomain they requested (wwww.yourdomain.com or even anyname.yourdomain.com) without having setup specific directories or symlinks for those requests in advance, then you can use mod_rewrite to redirect their request for a non-existent mis-typed or random subdomain to a valid page at on your website.

To do this, create a text file named .htaccess in your /htdocs directory that contains the following 5 directives, one per line with no other linebreaks (the lines may appear to wrap in your browser now), where each line begins Rewrite...

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\..+\..*$ [NC]
RewriteCond /full/path/to/homedirectory/htdocs/%1 !-d
RewriteCond /full/path/to/homedirectory/htdocs/%1 !-l
RewriteRule ^(.*)$ http://www.yourwebsite.com/ [L,NS]


where "/full/path/to/homedirectory" is the full path to your home directory as described after the 2nd paragraph here, and where "www.yourwebsite.com" leads to an area of your website for which you have properly created files in your /htdocs/www directory.


Last update: 2010-10-03 16:43
Author: FAQ Admin
Revision: 1.2

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 5 (1 Vote)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
When using virtual subdomains (where no directory or symlink for the subdomain in /htdocs exists), there might be some circumstances where you would want to add a %1 to the end of the last rule:

RewriteRule ^(.*)$ http://www.yourwebsite.com/%1

That will take a request for a non-existent subdomain such as "anything":

http://anything.yourdomain.com

and redirect it to:

http://www.yourdomain.com/anything

Of course, then you will have to have a file or directory named "anything" in /htdocs/www/ in order to handle that request properly. If not, then you should probably change the last rule to:

RewriteRule ^(.*)$ http://www.yourwebsite.com/script.php?sub=%1

and then have a script at /htdocs/www/script.php that is programmed to find the GET variable $sub or $_GET["sub"] and perform any actions you want.
Added at: 2003-04-12 22:38

Comment of Anonymous:
If you don't have your own domain name and are hosting a modwest.com subdomain to start with (like you.modwest.com) then you should change the first line of the Rewrite rules to:

RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\..+\..+\..*$ [NC]

That will make it so that going to http://you.modwest.com will not invoke any rewriting, while http://something.you.modwest.com will.
Added at: 2003-04-12 23:09

Comment of Anonymous:
Another idea entirely on how to make a catch-all for subdomains that have not been setup is to use the ErrorDocument directives in an /htdocs/.htaccess file. You could have 404 errors result in the user being served a PHP script that has this code:



That way, the PHP script (served as a 404 Not Found page) will send the browser wherever you want it to go when a subdomain that does not exist has been requested.
Added at: 2003-04-12 23:15