homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod_rewrite and subdomains?
Is it possible to translate non-existing subdirs into the following?

 2:05 pm on May 14, 2003 (gmt 0)

Is it possible to translate non-existing subdirs into the following?

I want


to be translated into


Where [subdir.domain.com...] is a non existing subdomain. Is this possible? And if so, how?



 2:38 pm on May 14, 2003 (gmt 0)


This all depends on whether your hosted account allows access to any subdomain of your domain other than "www" (whether it exists or not), and whether your DNS server is set up to handle additional subdomains - so-called "wild-card" DNS.

Also, what do you want to do with subdomains that do exist - are you currently handling them separately, and if so, how many are there?

Any rewrite solution will have to comprehend all subdomains of your domain, and handle existing and non-existing subdomains in a coordinated fashion.

Basically, the rewrite will have to recognize all existing subdomains and handle them appropriately, and then if no existing subdomains are recognized, default to a handler for those which do not exist. Unless there are many existing subdomains, this is not difficult, but like all mod_rewrite problems, it must be well-defined.



 11:34 am on May 15, 2003 (gmt 0)

Allright, this makes sense...

There are no existing subdomains, except for the "www" subdomain. There will be 100's of non-existing subdomains, that's why I wanted to use mod_rewrite. We have set up DNS so that everything points to this www directory.


will point to the same physical file as


So how do I start from here on?


 2:53 pm on May 15, 2003 (gmt 0)


You didn't say whether you wanted the requested page passed through, but I assume this may be the case. Also, I'll provide examples with and without query string support, in case you need that, too. Each example is stand-alone, and includes the Options and RewriteEngine setup, either of which you may already have in your existing .htaccess file. Note that if the user requests the main domains, either "http://domain.com" or "http://www.domain.com", no action is performed at all, so these rewrites should not affect the operation of the core part of your site.

Due to variations in server set-up, and the possibility that there are typos in this code or that I've misinterpreted what you want to do, there's a good possibility that these won't work properly. I encourage you to follow the reference citation below, and become familiar with regular expressions and mod-rewrite before proceeding.

> [subdir.domain.com...] or [subdir.domain.com...] translated to [domain.com...] :

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com [NC]
RewriteRule .* http://www.domain.com/%2/ [R=301,L]

Rewrite [subdir.domain.com...] or [subdir.domain.com...] to [domain.com...] (anyfile.foo can be blank) :

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/%2/$1} [R=301,L]

Rewrite [subdir.domain.com...] or [subdir.domain.com...] to [domain.com...] (anyfile.foo and/or query_string can be blank) :

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/%2/$1%{QUERY_STRING} [R=301,L]

Experiment with the example that most meets your needs. Again, I encourage you to read this Introduction to mod_rewrite [webmasterworld.com] post and the mod_rewrite and regular expressions references cited in it.



 5:58 pm on May 15, 2003 (gmt 0)

Hi Jim,

Thanks for your help! I will experiment with these settings and will let you know. I already have -some- experience in mod_rewriting, but it's the regex part I have trouble with.

Again, thanks for your help!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved