【问题标题】:Apache dynamic wildcard host rewrite with dynamic subdomains使用动态子域重写 Apache 动态通配符主机
【发布时间】:2015-10-18 18:27:58
【问题描述】:

我目前正在将旧的 apache vhost(其中 当前为每个新版本创建一个)转换为可以处理所有子域的动态主机,因此我们没有一直创建一个新的。我需要帮助!我们正在使用 apache v2.2

目标

动态的所有东西。拥有一个虚拟主机来处理一组特定子域的所有重定向。网址如下,注意sub1branch动态的,可以是任何东西。

sub1.branch.sandbox.domain.com

目录结构如下:

/var/www/vhosts/branch/sub1.branch.sandbox.domain.com

正如你在上面看到的,目录结构有branch作为一个子目录,完整的url是另一个子目录的名称。

另外,url 中的/images/ 需要为每个域转发到shared-httpdocs/images

我目前拥有的虚拟主机

<VirtualHost *:80>
        ServerName branch.sandbox.domain.com
        ServerAlias *.branch.sandbox.domain.com

        VirtualDocumentRoot /var/www/vhosts/branch/%0

        Options Indexes FollowSymLinks

        # Rewrite Engine Stuff Here
        RewriteEngine On

        DirectoryIndex index.php

        # Assets needs to be forwarded - currently not working
        RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
        RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]

        # The HTTP dispatcher - currently not working
        RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
        RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
</VirtualHost>

我要复制的旧主机

这是我要转换的旧虚拟主机。这太可怕了,很混乱,我们的操作每次都必须创建一个新的 DNS 条目。真是笑话!我需要解决这个问题...

<VirtualHost *:80>
    # Notice how the stupid convention below will require a new DNS entry each time?
    ServerName sandbox.branch.domain.com
    ServerAlias sandbox.api.branch.domain.com

    DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs

    <Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs">
            allow from all
            order allow,deny
            # Enables .htaccess files for this site
            #AllowOverride All

            RewriteEngine On

            # Rewrite all non-static requests to go through the webapp
            RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
            RewriteRule .* - [L]

            # Rewrite everything else to go through the webapp
            RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]

    </Directory>

    <Directory  "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers">
        allow from all
    </Directory>

    # Allow us to rewrite to the webapp without it being in the webroot
    Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers

    # Get shared/ to point to the shared static resources
    Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs

</VirtualHost>

每次我们有一个新分支时都需要一个新的 DNS 条目,所以我试图通过提供一个动态子域虚拟主机来缓解这种情况(请参阅我目前拥有的虚拟主机)。我已经从无法匹配 url 中的 /images/ 到永久重定向循环。

我怎样才能实现我的目标?我知道这有点复杂。如果我做不到,我只需要编写一个脚本,每次都会生成一个新的虚拟主机,但是一个“正常工作”的动态虚拟主机会很棒。到目前为止,我已经花了两天时间,我不是系统管理员。非常感谢您的帮助。

我一直在使用的资源:

【问题讨论】:

    标签: apache mod-rewrite url-rewriting apache2 apache2.2


    【解决方案1】:

    这不是一个完整的答案,但评论太长了。

    重写规则中的%0%0%9)是对最后一个 RewriteCond 中捕获的反向引用。在我看来,您想要的是主机名。此外,您似乎错过了路径的“分支”部分。在资产的重写中,您还丢弃了文件名部分。

    # Assets needs to be forwarded - currently not working
    RewriteCond %{REQUEST_URI} ^/(css|js|images)/(.*)
    RewriteRule .*$ /var/www/vhosts/branch/%{HTTP_HOST}/shared-httpdocs/%1/%2$ [L]
    
    
    # The HTTP dispatcher - currently not working
    RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
    RewriteRule ^(.*)$ /var/www/vhosts/branch/%{HTTP_HOST}/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
    

    您还可以从带有 RewriteLog 和 RewriteLogLevel 指令的 mod_rewrite 专用日志记录中获得调试帮助。

    希望这会让你走得更远。

    【讨论】:

    • 这至少很有帮助,我不知道你可以为 url 中的变量做一个%{HTTP_HOST},所以知道这一点很有用。为此 +1 :-)
    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2010-10-19
    • 2014-03-29
    • 2011-09-30
    • 2014-12-30
    相关资源
    最近更新 更多