【发布时间】:2015-10-18 18:27:58
【问题描述】:
我目前正在将旧的 apache vhost(其中 当前为每个新版本创建一个)转换为可以处理所有子域的动态主机,因此我们没有一直创建一个新的。我需要帮助!我们正在使用 apache v2.2。
目标
动态的所有东西。拥有一个虚拟主机来处理一组特定子域的所有重定向。网址如下,注意sub1 和branch 是动态的,可以是任何东西。
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/ 到永久重定向循环。
我怎样才能实现我的目标?我知道这有点复杂。如果我做不到,我只需要编写一个脚本,每次都会生成一个新的虚拟主机,但是一个“正常工作”的动态虚拟主机会很棒。到目前为止,我已经花了两天时间,我不是系统管理员。非常感谢您的帮助。
我一直在使用的资源:
-
mod_rewrite official docs - 在
REWRITE_COND条件下显示基本知识 - Sub domain rewriting - 关于子域重写的问题
- Asset rewriting - 另一个关于重写图像/css/js 之类的东西的问题,这似乎对我不起作用
【问题讨论】:
标签: apache mod-rewrite url-rewriting apache2 apache2.2