I'm currently in the process of converting an old apache vhost, where one is currently created for each new release, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using apache v2.2.
目标
动态所有事物。拥有一个处理一组特定子域的所有重定向的虚拟主机。网址如下,请注意 sub1
和 branch
是动态并且可以是任何内容。
sub1.branch.sandbox.domain.com
其目录结构如下:
/var/www/vhosts/branch/sub1.branch.sandbox.domain.com
正如您在上面所看到的,目录结构将 branch
作为子目录,完整的 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/
变成了永久重定向循环。
我怎样才能实现我的目标?我知道这有点复杂。如果我做不到,我只需要编写一个脚本,每次都会生成一个新的虚拟主机,但一个“正常工作”的动态脚本将是非常棒的。到目前为止我已经花了两天时间,我不是系统管理员。我们将非常感谢您的帮助。
我一直在使用的资源:
- mod_rewrite official docs - 使用
REWRITE_COND
显示诸如条件等基础知识
- Sub domain rewriting - 关于子域名重写的问题
- Asset rewriting - 另一个关于重写图像/css/js之类的问题,这似乎对我不起作用
最佳答案
这不是一个完整的答案,但太长了,无法发表评论。
重写规则中的 %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 专用日志记录中获得调试帮助。
希望这能让您走得更远。
关于Apache动态通配符主机用动态子域重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672249/