虚拟子域:每个用户一个子域 [英] virtual subdomain: one subdomain per user

查看:112
本文介绍了虚拟子域:每个用户一个子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我正在使用虚拟主机,因此我的用户可以拥有虚拟域,例如"user1.mydomain.com","user2.mydomain.com",...

On my website i'm using virtual host so my users can have virtual domain like 'user1.mydomain.com', 'user2.mydomain.com',...

问题在于,在虚拟域(例如"user1.domain.com")上,索引页面始终与我的索引页面"http://mydomain.com"上的页面相同.

The problem is that on virtual domains like 'user1.domain.com' the index page is always the same as on my index page 'http://mydomain.com'.

我想做的是为域和子域设置两个不同的索引页.我的问题是,如何将子域重定向到"index2.php"(例如),又如何使子域看起来像"user1.mydomain.com"?

What i want to do is to have two different index pages for the domain and for subdomains. My question, how to have subdomains redirected to 'index2.php' (for example) and still have the subdomains to look like 'user1.mydomain.com'?

推荐答案

理想情况下,您将为每个子域设置完全独立的文档目录. apache中的vhost是要走的路.但是,如果您想按自己的方式进行操作(子域重定向到单个文件),则需要做更多的工作,但仍然可行.首先,用通配符定义mod_vhost:

Ideally, you would set up completely separate document directories for each subdomain. vhost in apache is the way to go. If you however want to do it your way (subdomains redirecting to individual files), then it's a bit more work, but is still doable. First, define the mod_vhost with wildcard:

<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.mydomain.com
    ServerAlias *.mydomain.com

    ...
</VirtualHost>

然后在VirtualHost设置中使用mod_rewrite重写规则:

Then inside this VirtualHost setup rewrite rules using mod_rewrite:

<Location "/">
    RewriteCond %{HTTP_HOST} ^user1.mydomain.com$
    RewriteRule ^\/$ http://www.mydomain.com/index2.php [R=301,L]
    RewriteRule ^\/index.php$ http://www.mydomain.com/index2.php [R=301,L]

    RewriteCond %{HTTP_HOST} ^user2.mydomain.com$
    RewriteRule ^\/$ http://www.mydomain.com/index3.php [R=301,L]
    RewriteRule ^\/index.php$ http://www.mydomain.com/index3.php [R=301,L]

    ...
</Location>

但是请注意,这仅适用于对子域的//index.php请求.如果您打算做更多的事情,那么最好为每个子域设置单独的文档根目录.

Note however that this will only work properly for / and /index.php requests to subdomains. You are much better off setting up separate document root directories for each subdomain if you intend to do anything more than this.

这篇关于虚拟子域:每个用户一个子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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