通过htaccess进行子域屏蔽 [英] Sub-Domain Masking via htaccess

查看:56
本文介绍了通过htaccess进行子域屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑

我在服务器上设置了通配符子域dns区域,并希望每个子域都使用.htaccess和mod-rewrite加载/users/目录的内容.

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.

我希望URL在浏览器栏中保持不变,以便显示:

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

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

同时显示以下内容:

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

此外,如果要访问URL的www版本,我想将其重定向(更改URL)到子域版本.

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

我不太熟悉mod-rewrite规则,对您的帮助将不胜感激!

I'm not overly familiar with mod-rewrite rules and any help would be greatly appreciated!

谢谢.

推荐答案

尝试以下方法:

Options +FollowSymLinks
RewriteEngine On

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

如果用户访问tom.domain.com/directory1/directory2/directory3/file.html,他将被重定向(URL将在浏览器中更改)到www.domain.com/users/tom/directory1/directory2/directory3/file.html.

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.

更新: 要在浏览器中保持URL不变:

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]

尽管有一个问题:如果directory1将被称为users(例如tom.domain.com/users/directory2/directory3/file.html),那么这将不起作用-这就是您要为重写付出的代价. 解决方案:将users重命名为唯一的内容(例如users_dir_123_abc等-www.domain.com/users_dir_123_abc/tom/users/directory2/directory3/file.html).

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

这篇关于通过htaccess进行子域屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆