Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

my .htaccess code problem

code is doing well but got messed

4:37 am on Mar 22, 2007 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2006
votes: 0


i am using the following code to redirect any subfolder to subdomain
if someone types [test.domain.com...] then it will point to [domain.com...]
so it looks like a subdomain

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.htaccess$ - [f]

RewriteCond %{HTTP_HOST}!^www.domain.com
RewriteCond %{HTTP_HOST}!^ftp.domain.com
RewriteCond %{HTTP_HOST}!^mail.domain.com

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

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

now the problem is below
suppose two subdomains are there, i meant that to folders
domain.com/sd1 and domain.com/sd2
both are working well as
sd1.domain.com and sd2.domain.com

now suppose, i m goping to make one folder under sd1
i give the name 'sd2'
so url looks like sd1.domain.com/sd2/
but it will not work because i have already one subdomain named sd2
so anything under sd1.domain.com/sd2/ will be treated as domain.com/sd2/
my code ignores the subdomain part

anyone who knows this stuff please help me

1:04 pm on Mar 22, 2007 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
votes: 0

Good post! -- The problem is well-described.

There is no "perfect" solution, as all you can do is to minimize the chances for for "interference" by picking "likely-unique" subdomain-subdirectory names. You can either prepend a "tag" on the subdomain-subdirectory filepaths to distinguish them from main-domain subdirectories, or you can move all subdomain-subdirectories into one uniquely-named subdirectory, and put all the subdomain-subdirectories there.

As examples:

RewriteCond %{HTTP_HOST} !^(www¦ftp¦mail)\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subd_%1/$1 [L]

Here, each subdomain is stored in a subdirectory with the prefix "subd_", so the file for URL foo.example.com/bar would be stored at example.com/subd_foo/bar
As long as "subd_bar" is not allowed as a "real" subdirectory under example.com, you're OK. You have to be careful not to allow creation of non-subdomain subdirectories in example.com that start with "subd_".

RewriteCond %{HTTP_HOST} !^(www¦ftp¦mail)\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sub-d/%1/$1 [L]

Here, we're doing almost the same thing, except that files for each subdomain are stored under a subdirectory named "/sub-d". So the file for subdomain "foo.example.com/bar" would be stored at "example.com/sub-d/foo/bar" Here, your "exposure" to intereference is limited to one subdirectory name only.

Note that I compressed your three RewriteConds to one by using the "local OR", and escaped the literal periods in the regular expressions as recommended.

Also, replace the broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.


5:46 am on Mar 23, 2007 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2006
votes: 0

Thanks a lot jdMorgan for this gr8 solution

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members