【问题标题】:How to match dynamic sub-domain alias to sub-folder on apache server如何将动态子域别名与 apache 服务器上的子文件夹匹配
【发布时间】:2019-01-09 01:01:57
【问题描述】:

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

我已经完成了使用通配符配置服务器:例如。 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>

【问题讨论】:

  • stackoverflow.com/questions/11388618/… 我想这就是你要找的。​​span>
  • 这个问题描述了如何将通配符子域重定向到根域。我有兴趣将子域重定向到它们各自的根文件夹。不过还是谢谢。
  • 尝试使用 .htaccess
  • 请分享您当前虚拟主机的 Apache 配置。

标签: php linux apache dns subdomain


【解决方案1】:

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

    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>  

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

<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>  

非常感谢您的贡献。

【讨论】:

    猜你喜欢
    • 2014-12-30
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2015-02-02
    • 2020-07-25
    • 2021-11-18
    • 2022-11-02
    • 2018-06-30
    相关资源
    最近更新 更多