Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何自动将子域映射到子文件夹_Php_Xampp_Virtualhost - Fatal编程技术网

Php 如何自动将子域映射到子文件夹

Php 如何自动将子域映射到子文件夹,php,xampp,virtualhost,Php,Xampp,Virtualhost,我使用一个PHP脚本,在每个用户注册时为他创建一个子文件夹。e、 g:domain.com/users/user1 我需要将子域映射到这些子文件夹,例如:user1.domain.com。 我正在使用XAMPP,并且我已经遵循了它,它运行得很好 但我必须为每个用户/子域这样做 我需要在用户注册时自动执行此操作。您希望在Apache上执行大规模虚拟主机托管。在这里,您可以找到如何做到这一点的信息: 根据您链接的教程中的示例: NameVirtualHost * <VirtualHost

我使用一个PHP脚本,在每个用户注册时为他创建一个子文件夹。e、 g:domain.com/users/user1

我需要将子域映射到这些子文件夹,例如:user1.domain.com。

我正在使用XAMPP,并且我已经遵循了它,它运行得很好

但我必须为每个用户/子域这样做


我需要在用户注册时自动执行此操作。

您希望在Apache上执行大规模虚拟主机托管。在这里,您可以找到如何做到这一点的信息:

根据您链接的教程中的示例:

NameVirtualHost *
  <VirtualHost *>
    DocumentRoot "C:\xampp\htdocs"
    ServerName localhost
  </VirtualHost>
  <VirtualHost *>
    DocumentRoot "C:\Documents and Settings\Me\My Documents\clientA\website"
    ServerName clientA.local
  <Directory "C:\Documents and Settings\Me\My Documents\clientA\website">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
<VirtualHost *>
    DocumentRoot "C:\Documents and Settings\Me\My Documents\clientB\website"
    ServerName clientB.local
  <Directory "C:\Documents and Settings\Me\My Documents\clientB\website">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
要在localhost上测试这一点,您可以在
hosts
文件中手动设置一些子域。查看为什么无法在
hosts
文件中设置通配符子域

不要忘记在httpd.conf中加载vhost_alias_模块:

LoadModule vhost_alias_module modules/mod_vhost_alias.so
然后,您将替换vhost配置,如以下示例所示:

<VirtualHost *>
    # get the server name from the Host: header
    UseCanonicalName Off

    # this log format can be split per-virtual-host based on the first field
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog logs/access_log vcommon

    # include the server name in the filenames used to satisfy requests
    VirtualDocumentRoot "C:/Documents and Settings/Me/My Documents/%1/website"

    <Directory "C:/Documents and Settings/Me/My Documents/*/website">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
        DirectoryIndex  index.php index.html index.htm
    </Directory>
</VirtualHost>

#从主机:标头获取服务器名称
UseCononicalName关闭
#此日志格式可以根据第一个字段按虚拟主机拆分
日志格式“%V%h%l%u%t\%r\%s%b”vcommon
自定义日志/访问日志vcommon
#在用于满足请求的文件名中包含服务器名称
VirtualDocumentRoot“C:/Documents and Settings/Me/My Documents/%1/网站”
选项索引跟随符号链接
允许超越所有
命令允许,拒绝
通融
要求所有授权
DirectoryIndex.php index.html index.htm

这将使用通配符设置所请求子域的文档根。

我通过使用服务器上的默认vhost实现了类似的功能。假设您有dns设置,所有子域都指向相应的IP地址,那么它们都应该解析为默认vhost。默认vhost指向的文件夹也需要一个index.php文件,该文件利用子域提供适当的内容


通过编辑/etc/hosts文件并将本地dns中的子域指向本地主机,您可以使用XAMPP在本地复制此文件。将webroot设置为index.php文件所在的位置,并从$\u服务器变量获取域名。从那里,您可以通过子域确定用户,并以编程方式显示内容。

您可以安装一个类似的或来承担此任务。您将使用一个友好的GUI来配置子域,所有的基本配置都将在后台进行。这是真实世界的主机提供商所使用的。

我已经考虑了一段时间,但从未测试过它。 我认为您应该尝试与某些MVC框架(如CodeIgniter)相同的方法

使用index.php路由所有请求。 获取$\u服务器[“HTTP\U主机”]并对其进行解析以获取您的子域。
现在,根据您的子域加载对应视图(与MVC中的视图相同)

为什么要为每个用户设置子域?目的何在?webhosting?@LucasF是的,webhosting这个问题应该发布在……我知道这可能需要做很多工作,但请尝试将文档中的至少一个基本示例嵌入到您的答案中,以防链接发生变化(希望不会发生,因为它是apache2文档,但谁知道呢)所以有一些具体的东西将来其他人可以特别评论。@LucasF我以前读过这个话题,我不能完全理解它,也不能在我的网站上应用它。你能在这里简化一下吗。
<VirtualHost *>
    # get the server name from the Host: header
    UseCanonicalName Off

    # this log format can be split per-virtual-host based on the first field
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog logs/access_log vcommon

    # include the server name in the filenames used to satisfy requests
    VirtualDocumentRoot "C:/Documents and Settings/Me/My Documents/%1/website"

    <Directory "C:/Documents and Settings/Me/My Documents/*/website">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
        DirectoryIndex  index.php index.html index.htm
    </Directory>
</VirtualHost>