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

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

问题描述

我希望创建一个系统,该系统在注册时将在我的网站上为用户帐户区域创建一个子域.

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

例如johndoe.website.com

e.g. johndoe.website.com

我认为这与 .htaccess 文件有关,并且可能会重定向到网站上的另一个位置?我其实不知道.但任何让我开始的信息将不胜感激.

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.

推荐答案

快速纲要

  1. 您需要在您的 DNS 服务器 *.website.com 上创建一个通配符域
  2. 然后在您的 vhost 容器中,您还需要指定通配符 *.website.com - 这是在 ServerAlias DOCs
  3. 然后在PHP中提取并验证子域并显示相应的数据

长版

1.创建通配符 DNS 条目

在您的 DNS 设置中,您需要创建一个通配符域条目,例如 *.example.org.通配符条目如下所示:

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.在 vhost 中包含通配符

接下来在 Apache 配置中,您需要设置一个 vhost 容器,该容器在 ServerAlias DOCs 指令.一个示例 vhost 容器:

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.找出您在 PHP 中所在的子域

然后在您的 PHP 脚本中,您可以通过查看 $_SERVER 超级全局变量来找出域.这是在 PHP 中抓取子域的示例:

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];
}

我在这里使用了正则表达式来允许人们通过 www.subdomain.example.org 或 subdomain.example.org 访问您的网站.

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

如果您从未预料到必须处理 www.(或其他子域)然后您可以简单地使用这样的子字符串:

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.

我之前在 帖子中记录了我的基于 mod_rewrite 的大规模虚拟主机环境博客,如果这是您希望采取的路线,您可以查看该博客.当然,还有 各自的 Apache 手册页.

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 也有一种内部处理大量虚拟主机的方法,它比我使用的 mod_rewrite 方法稍微不那么灵活.Apache 动态配置的大规模虚拟主机手册页.

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天全站免登陆