Home
 

Customer Support

Search for keywords:

Browse by category:

Can I have a catch-all subdomain?

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.

User-Contributed Notes

add a note
12-Apr-2003 22:38
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.

12-Apr-2003 23:09
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.

12-Apr-2003 23:15
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:

<?  header("Location: http://www.somesite.com/somepage";); ?>

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.

add a note

Related Questions:


Why does it say "directory not empty" when I try to remove a directory?

How do I create subdomains?

How do I get telnet or SSH access to the server?

What is the real full path to my home directory?

Where do I put my webpages and files?

How do I upload and edit my files?

Why do I keep getting old copies of my pages when I just updated and changed my files?

How do I change my password?

When will my site be activated?

How do I get to one of my subdomains from a temp.modwest.com or secure.modwest.com URL?

Can I switch my domain name or alter my administrator username?

How do I recreate the "_" symlink that directs mydomain.com to www.mydomain.com?

Why do I Get an Error Page When Accessing Webmail with Internet Explorer?

What kind of resource limits do you have for your shared hosting environment?

Can I display a "Secured by RapidSSL" seal on my site?

Browse Categories:

Getting Started, FTP, Telnet/SSH, Moving Domains, E-mail, Traffic Reports, Mailing Lists, Apache, PHP, CGI, Other Server-Side Scripting, MySQL Database, Imaging Libraries, Other Software, Billing & Terms, Control Panel, E-commerce, Pre-Sales


Tiny Modwest Logo         Copyright 2000-2008 by Modwest, Inc.          About Us    |    Blog    |    Jobs    |    Web Design    |    Contact Us