gpt4 book ai didi

php - 如何将动态子域别名与 apache 服务器上的子文件夹匹配

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:06 25 4
gpt4 key购买 nike

我目前正在使用 PHP 开发一个基于 SaaS Web 的项目,该项目要求用户拥有唯一的子域才能使用该应用程序。

我已完成使用通配符配置服务器:例如。 user.example.com ==> *.example.com。该应用程序在 /var/www/html/ 下为用户创建一个子文件夹,其中包含用户名,例如。 user.example.com 将在 /var/www/html/user/ 中。

我坚持让用户的链接没有基本文件夹,例如 user.example.com 指向 /var/www/html/user/ 没有包括 http://user.example.com/user

我已经尝试过虚拟主机和别名,但似乎我必须手动分配不适用于该项目的变量。

我只需要子域指向它们各自的子文件夹。

我的apache配置文件

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin admin@xxxxxxx.tdl
DocumentRoot /var/www/html/%1/
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
ErrorDocument 404 /error_404.html
</VirtualHost>

最佳答案

经过研究,我终于找到了解决方案。我意识到我没有激活别名模块,所以我激活如下:

    sudo a2enmod vhost_alias

我还注意到,将每个子域提供给其各自的子文件夹需要一个动态配置脚本来通过目录名称插值自动选择 VirtualDocumentRoot,如下所示:

<VirtualHost *:80>
ServerAdmin admin@vspace.ke
DocumentRoot /var/www/html/
VirtualDocumentRoot /var/www/html/%1/
<Directory "/var/www/html/">
Options FollowSymLinks
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>

我还注意到您必须在主机配置中声明所有子域和非子域:I.Ewww 的别名

<VirtualHost *:80>
ServerName www.vspace.ke
ServerAlias www
DocumentRoot /var/www/html/
<Directory /var/www/html/>
Options +FollowSymLinks
</Directory>
</VirtualHost>

没有子域的别名

<VirtualHost *:80>
ServerName vspace.ke
ServerAlias
DocumentRoot /var/www/html/
<Directory /var/www/html/>
Options +FollowSymLinks
</Directory>
</VirtualHost>

和*(通配符)子域

<VirtualHost *:80>
ServerAdmin admin@vspace.ke
DocumentRoot /var/www/html/
VirtualDocumentRoot /var/www/html/%1/
<Directory "/var/www/html/">
Options FollowSymLinks
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>

非常感谢您的贡献。

关于php - 如何将动态子域别名与 apache 服务器上的子文件夹匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com