Welcome to WebmasterWorld Guest from 50.16.78.128

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod_rewrite and subdomains?

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

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

10+ Year Member



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

I want

[subdir.domain.com...]

to be translated into

[domain.com...]

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)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



designaweb,

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.

Jim

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

10+ Year Member



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.

this.domain.co.uk/index.html

will point to the same physical file as

www.domain.co.uk/index.html

So how do I start from here on?

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



designaweb,

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.

HTH,
Jim

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

10+ Year Member



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!