无法使用httpd-vhosts.conf从子域重定向到子子域

时间:2017-12-15 21:14:54

标签: php apache .htaccess redirect subdomain

我正在尝试设置一个本地开发服务器,我只需访问目录E:\development\projects\中的任何项目,并让apache自动将我重定向到正确的子域。

但是,当尝试重定向到子子域(backend.project01.dev01)时,我只是收到“服务器无法访问”消息。

有关详细信息,请参阅我的httpd-vhosts.conf:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName dev01
    ServerAlias www.dev01
    DocumentRoot "E:\development\projects"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects">
    </directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName dev01
    ServerAlias *.dev01
    VirtualDocumentRoot "E:\development\projects\%1"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects\%1">
    </directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName dev01
    ServerAlias *.*.dev01
    VirtualDocumentRoot "E:\development\projects\%2"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects\%2">
    </directory>
</VirtualHost>

简而言之:

这项工作: project01.dev01/

这不起作用: backend.project01.dev01/

1 个答案:

答案 0 :(得分:1)

所以我找到了解决方案。要匹配子子域,您必须先匹配deepest级别。

I.e。:在deepest.sub.domain

之前匹配sub.domain

这个例子中的答案是:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName dev01
    DocumentRoot "E:\development\projects"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects">
    </directory>
</VirtualHost>

## moved this up
<VirtualHost *:80>
    ServerName dev01
    ServerAlias *.*.dev01
    VirtualDocumentRoot "E:\development\projects\%2"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects\%2">
    </directory>
</VirtualHost>

## moved this down
<VirtualHost *:80> 
    ServerName dev01
    ServerAlias *.dev01
    VirtualDocumentRoot "E:\development\projects\%1"
    ErrorLog "logs\errors.log"
    <directory "E:\development\projects\%1">
    </directory>
</VirtualHost>

在此示例中,apache将始终首先尝试匹配*.*.dev01,如果失败,请尝试匹配*.dev01

希望这将有助于其他人。