【发布时间】:2013-11-30 23:17:24
【问题描述】:
我有许多站点在同一台机器上运行,由 httpd 提供服务。每个站点都设置为不同子域上的 VirtualHost。此外,对于每个子域,在不同的端口上有两个 VirtualHost:一个用于“稳定”版本,一个用于“测试”版本。稳定版托管在 80 端口上。
登录(稳定版)网站后,如果后端确定用户应该使用测试版,则会设置一个 cookie。
我希望 Apache 在对稳定版本的后续请求中检测此 cookie,并(在用户不知情的情况下)将请求重定向到 beta 版本。
我怎样才能做到这一点?
我已经尝试在 httpd.conf 中使用 RewriteCond / RewriteRule,但它似乎没有任何效果 - 也许 Apache 忽略它而支持匹配的 VirtualHost,或者对排序很敏感(我认为 VirthalHost 定义是被首先包括在内)?也许我应该使用 mod_proxy,无论如何?
配置
我在下面的配置中包含(匿名)sn-ps
httpd.conf
Listen 80
NameVirtualHost *:80
Listen 81
NameVirtualHost *:81
Include "/path/to/checkout/config/[stage]/apache2/*.conf" # VirtualHosts of sites
Include "/path/to/checkout/config/common/apache2/*.conf" # My attempted redirect
config/[stage]/apache2/[site]/apache2/[app-stable].conf
<VirtualHost *:80>
ServerName [app.hostname]
ServerAlias [alternative-app-name.hostname]
DocumentRoot "/var/www-application/release-base/current/app/public"
<Directory "/var/www-application/release-base/current/app/public/">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
<!-- snip -->
</VirtualHost>
config/[stage]/apache2/[site]/apache2/[app-beta].conf
<VirtualHost *:81>
ServerName [app.hostname]
ServerAlias [alternative-app-name.hostname]
DocumentRoot "/var/www-application/beta-release-base/current/app/public"
<Directory "/var/www-application/beta-release-base/current/app/public/">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
<!-- snip -->
</VirtualHost>
config/common/apache2/common.conf
# !!! This has no effect !!!
RewriteEngine On
RewriteCond %{HTTP_COOKIE} cookiename
RewriteCond %{SERVER_PORT} !8081
RewriteRule ^ https://%{HTTP_HOST}:8081%{REQUEST_URI} [R,L] # (R for debugging)
App .htaccess(如果相关)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
【问题讨论】:
标签: apache mod-rewrite virtualhost