即时创建子域的.htaccess用(PHP) [英] Create Subdomains on the fly with .htaccess (PHP)

查看:604
本文介绍了即时创建子域的.htaccess用(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待建立一个系统,该系统上注册将在我的网站上创建一个子域帐户的用户区。

例如。 johndoe.website.com

我认为这将是什么做的.htaccess文件和可能将重定向到另一个位置,在网站上?我真的不知道。但是,任何的信息,开始我关闭将大大AP preciated。

创建一个标志区面积不是问题 - 我已经这样做了很多次。我只是不能确定从哪里开始的子域。

谢谢,本。

解决方案

的快速纲要

  1. 您需要将您的DNS服务器上创建一个通配符域* .website.com
  2. 然后在你的虚拟主机的容器,你需要指定通配符藏汉* .website.com - 这是在的 ServerAlias​​ 的DOCs
  3. 然后提取和验证在PHP中的子域,显示适当的数据

的长版

1。创建一个通配符DNS条目

在您的DNS设置,你需要创建一个通配符域条目 *。例如.ORG 。通配符项看起来是这样的:

  *。example.org。 3600 127.0.0.1
 

2。包括在虚拟主机的通配符

接下来在你需要建立一个虚拟主机的容器,它指定的 ServerAlias​​ 的DOCs 指令。一个例子虚拟主机容器:

 <虚拟主机*:80>
  服务器名server.example.org
  ServerAlias​​ * .example.org
  UseCanonicalName关闭
< /虚拟主机>
 

3。制定出哪些子域名你是在PHP

然后在你的PHP脚本,你可以通过查看 $ _ SERVER 超全局变量,找出域。这是敛子域在PHP的一个例子:

  preg_match('/([^。] +)\例如\ .ORG /',$ _ SERVER ['SERVER_NAME'],$匹配);
如果(使用isset($匹配[1])){
    $子域= $匹配[1];
}
 

我已经使用正则表达式在这里,让人们通过www.subdomain.example.org或subdomain.example.org打你的网站。

如果你从来没有预料不必处理WWW。 (或其他子域),那么你可以简单地使用像这样的子串:

  $子域= SUBSTR(
                 $ _ SERVER ['SERVER_NAME'],0,
                 strpos($ _ SERVER ['SERVER_NAME'],'。')
             );
 

海量虚拟主机

海量虚拟主机是一个稍微不同的方案上面的,你通常会用它来承载的许多不同的网站,而不是试图用它功率的应用程序的问题提出。

我以前也证明我的mod_rewrite根据群众虚拟主机环境中职位上我的博客 ,你可以看看,如果这是你希望走的路线。还有,当然相应的Apache手册页

阿帕奇也有应对大规模虚拟主机,它比我使用了mod_rewrite的方法略有弹性较小的内部方法。这是关于Apache 动态配置的大规模虚拟主机手册页面描述的。

I am looking to create a system which on signup will create a subdomain on my website for the users account area.

e.g. johndoe.website.com

I think it would be something to do with the .htaccess file and possibly redirecting to another location on the website? I don't actually know. But any information to start me off would be greatly appreciated.

Creating a sign up area is not the problem - i have done this many a time. i am just unsure where to start with the subdomain.

Thanks, Ben.

解决方案

The quick rundown

  1. You need to create a wildcard domain on your DNS server *.website.com
  2. Then in your vhost container you will need to specify the wildcard aswell *.website.com - This is done in the ServerAlias DOCs
  3. Then extract and verify the subdomain in PHP and display the appropriate data

The long version

1. Create a wildcard DNS entry

In your DNS settings you need to create a wildcard domain entry such as *.example.org. A wildcard entry looks like this:

*.example.org.   3600  A  127.0.0.1

2. Include the wildcard in vhost

Next up in the Apache configuration you need to set up a vhost container that specifies the wildcard in the ServerAlias DOCs directive. An example vhost container:

<VirtualHost *:80>
  ServerName server.example.org
  ServerAlias *.example.org
  UseCanonicalName Off
</VirtualHost>

3. Work out which subdomain you are on in PHP

Then in your PHP scripts you can find out the domain by looking in the $_SERVER super global variable. Here is an example of grabbing the subdomain in PHP:

preg_match('/([^.]+)\.example\.org/', $_SERVER['SERVER_NAME'], $matches);
if(isset($matches[1])) {
    $subdomain = $matches[1];
}

I have used regex here to to allow for people hitting your site via www.subdomain.example.org or subdomain.example.org.

If you never anticipate having to deal with www. (or other subdomains) then you could simply use a substring like so:

$subdomain = substr(
                 $_SERVER['SERVER_NAME'], 0,
                 strpos($_SERVER['SERVER_NAME'], '.')
             );

Mass Virtual Hosting

Mass virtual hosting is a slightly different scheme to the above in that you would usually use it to host many distinct websites rather than attempting to use it power an application as the question proposes.

I have documented my mod_rewrite based mass virtual hosting environment before in a post on my blog, which you could look at if that is the route you wish to take. There is also, of course, the respective Apache manual page.

Apache also has an internal way of dealing with mass virtual hosting that is slightly less flexible than the mod_rewrite method I have used. This is all described on the Apache Dynamically Configured Mass Virtual Hosting manual page.

这篇关于即时创建子域的.htaccess用(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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