Welcome to WebmasterWorld Guest from 54.163.40.152

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

pls help to rewrite subdomains to subfolders

I want to rewrite subdomains to subfolders using my .htaccess file

     

enil

5:15 am on Nov 21, 2006 (gmt 0)

5+ Year Member



hi 2 all,

i want the below stuff to run well

i have wildcard dns set up successfully on my server under domain.com.
now i want this:
if any type [test.domain.com...]
then this should go to [domain.com...]
and url will remain same as [test.domain.com...] in browser

i found one solution as below in .htaccess file :
---------------------------------------------------------------
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.htaccess$ - [f]

RewriteCond %{HTTP_HOST}!^www.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+).domain.com
RewriteRule ^$(.*) /%1/$1 [L]
---------------------------------------------------------------

it works with [test.domain.com...]
it is opening index.htm file under test folder
but
when i try to open [test.domain.com...] directly
it is redirected to [domain.com...]

rest all files are works directly except index.htm

so anyone who knows this stuff pls help me
any new code will be appreciated...

Thanks
-ENIL

enil

5:30 am on Nov 21, 2006 (gmt 0)

5+ Year Member



also one more problem i forgot to state

the above .htaccess code works sometime and sometime do not.
when i change something and undo that change then it will not work

sometimes it directly open files and sometimes do not
i.e. [test.domain.com...] works
if i make any change in .htaccess then undo that change then above url will not work

also links on that abcd.htm page is not working too

Thanks
-ENIL JEEYANI

jdMorgan

2:01 pm on Nov 21, 2006 (gmt 0)

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



There are problems at several levels in that code. First, literal periods in regular-expressions patterns need to be escaped by preceding them with a "\". Otherwise, they are taken as regex tokens which mean, "any single character".

Next, the pattern in the RewriteRule was incorrect. I suspect you intended "^(.*)$" but this in itself contains unnecessary characters and can be simplified.

Finally, if this code is to be used in .htaccess, then you must take steps to prevent recursion. The easiest way to do it is to name your subdomain-subdirectories with a unique prefix, shown here as "sd_". If this is not done, then test.example.com/foo.html will be rewritten to /test/foo.html, that will be rewritten to /test/test/foo.html, rewritten again to /test/test/test/foo.html, etc. This will only stop when the server reaches its maximum redirection limit.

Tagging the subdirectories and testing for the presence of that unique tag prevents this looping. As shown, test.example.com/foo.html will be rewritten to /sd_test/foo.html.

The tag also clearly identifies which subdirectories are for subdomains, and which are for other purposes such as shared images, CSS, scripts, etc.


RewriteRule [b]^\.h[/b]taccess$ - [F]
#
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_%1/$1 [L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

[edited by: jdMorgan at 2:03 pm (utc) on Nov. 21, 2006]

jdMorgan

2:05 pm on Nov 21, 2006 (gmt 0)

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



Also, always flush your browser cache after making any changes to your server configuration code, and before testing. Otherwise your browser will use the cached pages and responses, giving you misleading test results.

Jim

rkrause

6:10 pm on Nov 22, 2006 (gmt 0)

5+ Year Member



Here's an alternative solution. It bypasses the need for specially-named subdirectories and it creates an environment variable with the subdomain name for later reference.

This solution avoids the 'directory occlusion' side-effect of the above example. (That is, nominally, if the the REQUEST_URI references a directory path with the same name as the current subdomain's directory path, then that subdirectory should still be processed by the rewrite rule). This solution also allows you to easily determine whether the page was accessed via the subdomain (by simply checking the environment variable, SUBDOMAIN, in a CGI script or SSI page). Additionally, this solution verifies that the subdomain has its own directory, and, if not, then it defaults to the main website under the document root (in which case SUBDOMAIN will be set to an empty string).

RewriteBase /

#### URL Rewrite Handler for Subdomains (by Randall Krause) ####

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} ="" 
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.mydomain\.org\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule ^(.*) subdomains/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

Note, that I have tested this code, and it is almost identical to what I use on my production server which is why I have strict input validation (remember the HOST field can be forged with any falsified or corrupt data, so it is important to verify that it complies with the HTTP/1.1 specification, esp. if your primary site is on its own IP address, in which case Apache most likely hasn't even validated the HOST header). Note that on some shared-hosting configurations, I've found that DOCUMENT_ROOT may not be correct until the first rewriting phase is complete (for who knows what reason) so you may have to hard-code the path.

You will need to change "mydomain\.org" to your actual domain name. If your subdomain directories are to be located someplace other than "subdomains/%1", then you will want to modify the actual path to the subdomains based off of the document root.

--Randall

jdMorgan

7:57 pm on Nov 22, 2006 (gmt 0)

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



Very nice. I'm not sure I understand the wording in the 'directory occlusion' discussion, though. Can you elaborate on that, perhaps with examples?

Also, what code or module defines the envar REDIRECT_SUBDOMAIN?

Thanks,
Jim

jdMorgan

10:59 pm on Nov 22, 2006 (gmt 0)

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



As I stated above, the method that uses a unique prefix for all subdomain subdirectories is probably the easiest method to understand.

Here's another tested version using an environment variable to prevent the rewrite loop problem in .htaccess. This uses the same idea as rkrause contributed above, but with a few differences in style and function:


# if we have not already rewritten this request to a subdomain-subdirectory
RewriteCond %{ENV:REQ_SUBDOMAIN} ^$
# and if valid hostname, get requested subdomain into local variable %2
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][-a-z0-9]+)\.example\.com(:80)?$ [NC]
# and if the requested subdomain is not "www"
RewriteCond %{HTTP_HOST} !^www\.example\.com
# and if the subdirectory for this subdomain exists
RewriteCond %{DOCUMENT_ROOT}/addons/%2 -d
# then rewrite the request to the subdirectory, set the loop-prevention variable, & quit for this pass
RewriteRule (.*) /addons/%2/$1 [E=REQ_SUBDOMAIN:%2,L]

The main differences are an exclusion for www.example.com and an allowance for www.<subdomain>.example.com -- The basic idea is the same.

Jim

enil

5:53 am on Nov 24, 2006 (gmt 0)

5+ Year Member



your help is working now
thanks

thanks a lot again to jdMorgan and rkrause

rkrause

3:23 am on Nov 29, 2006 (gmt 0)

5+ Year Member



Very nice. I'm not sure I understand the wording in the 'directory occlusion' discussion, though. Can you elaborate on that, perhaps with examples?

Thanks. Well the original implementation prematurely abandons the rewriting phase as shown by the example below. The logic is faulty in that it has the side-effect of rejecting requested URIs despite them being perfectly valid. In this way directories are occluded (i.e. obstructed).

http://subdomain.example.com/sd_/

Also, what code or module defines the envar REDIRECT_SUBDOMAIN?

The source code I posted above creates the environment variable SUBDOMAIN. Apache then prefixes the name with REDIRECT_ after the internal redirect. Hence, I have to verify REDIRECT_SUBDOMAIN to prevent recursion.

--Randall

jdMorgan

3:49 am on Nov 29, 2006 (gmt 0)

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



OK, that's much clearer on both the occlucion and the operation of the code.

I would like to point out that I posted

The easiest way to do it is to name your subdomain-subdirectories with a unique prefix, shown here as "sd_"

So, the method is dirt-simple, and works well on servers where the person installing the code is aware that it is there, and knows that all "/sd_" subdirectories are "special." It would not work well on a server shared among multiple users each with different subdomains, any of whom might unpredictably decide to name one of their subirectories /sd_<something> and then find it unreachable. So despite my usual attitude of "simple is good," I'll concede that it's certainly not bullet-proof. :)

Jim

rkrause

4:08 am on Nov 29, 2006 (gmt 0)

5+ Year Member



Hi! Thanks for the quick reply. I just perused your new code above, but I'm not sure it will function as intended.

Btw, if you or anyone else is interested, I actually have a site which documents all the solutions I've developed for simulating virtual hosts inside of ".htaccess". It's a pretty comprehensive project with a lot of examples and explanations. I've been improving each method over the past several years, and I figure they should prove quite useful to other people. I don't think we're allowed to post links here, so drop me a PM.

--Randall

 

Featured Threads

Hot Threads This Week

Hot Threads This Month