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

查看:264
本文介绍了使用.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.

推荐答案

快速下载



The quick rundown


  1. 您需要在您的DNS服务器上创建通配符域* .website.com

  2. 然后在您的vhost容器中,您还需要指定通配符* .website.com - 完成在 ServerAlias DOCs

  3. 然后在PHP中提取并验证子域,并显示适当的数据

  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 as well *.website.com - This is done in the ServerAlias DOCs
  3. Then extract and verify the subdomain in PHP and display the appropriate data



长版本



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配置中,您需要设置一个在 ServerAlias DOCs 指示。一个示例虚拟容器:

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.

I在在我的博客上发帖之前,已经记录了我的基于mod_rewrite的大量虚拟主机环境a>,你可以看看,如果这是你想要的路线。当然,还有各自的Apache手册页< a>。

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