Sub-Domain Masking via htaccess

ε祈祈猫儿з 提交于 2019-12-12 09:07:58

问题


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 familiar with mod-rewrite rules and any help would be greatly appreciated!

Thank you.


回答1:


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).



来源:https://stackoverflow.com/questions/7587941/sub-domain-masking-via-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!