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

时间:2018-08-01 12:42:58

标签: php linux apache dns subdomain

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

我完成了使用通配符配置服务器的操作:例如。 user.example.com ==> *.example.com。该应用程序在/var/www/html/下为用户创建一个名为Eg的子文件夹。 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>

1 个答案:

答案 0 :(得分:0)

最后,经过研究,我找到了解决方案。我意识到我没有激活别名模块,因此我按如下所示进行了激活:

    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>  

非常感谢您的贡献。