使用 .htaccess (PHP) 即时创建子域

编程入门 行业动态 更新时间:2024-10-19 07:32:44
本文介绍了使用 .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

e.g. johndoe.website

我认为这与 .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.

推荐答案

快速纲要

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

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

    The long version 1. Create a wildcard DNS entry

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

    *.example. 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 ServerAlias *.example 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/', $_SERVER['SERVER_NAME'], $matches); if(isset($matches[1])) { $subdomain = $matches[1]; }

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

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

    如果您从未预料到必须处理 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) 即时创建子域

    本文发布于:2023-10-31 16:03:08,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1546580.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:htaccess   PHP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!