开发者

Sub-Domain Masking via htaccess

EDITED

I set-up a wildcard subdomain dns zone on my server and would like to have each subdomain load the contents of the /users/ direcotry using .htaccess and mod-rewrite.

I would like the URL to remain unaltered in the browser bar so that it shows:

http://username.domian.com/../../

while showing the contents of:

http://www.domain.com/users/username/../../

Additionally, I would like to redirect (change the URL) to the subdomain version if the www version of the URL is accessed.

I'm not overly fa开发者_Go百科miliar with mod-rewrite rules and any help would be greatly appreciated!

Thank you.


Try this one:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteCond %1 !=www
RewriteRule ^(.*)$ http://www.domain.com/users/%1/$1 [R=302,L]

If user will visit tom.domain.com/directory1/directory2/directory3/file.html he will be redirected (URL will be changed in browser) to www.domain.com/users/tom/directory1/directory2/directory3/file.html.

If that is not what you really after (as I'm a bit confused about this "masked" concept of yours) then please explain your requirements better so I can adjust the rule.


UPDATE: To keep URL unchanged in browser:

Options +FollowSymLinks
RewriteEngine On

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

There one problem though: if directory1 will be called users (e.g. tom.domain.com/users/directory2/directory3/file.html), then this will not work -- that is the price you have pay for rewriting. Solution: rename users to something unique (e.g. users_dir_123_abc etc -- www.domain.com/users_dir_123_abc/tom/users/directory2/directory3/file.html).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜