![使用 hosts 文件将子域映射到 Apache 服务器上的本地主机时,使用重写规则重定向用户](https://linux22.com/image/805441/%E4%BD%BF%E7%94%A8%20hosts%20%E6%96%87%E4%BB%B6%E5%B0%86%E5%AD%90%E5%9F%9F%E6%98%A0%E5%B0%84%E5%88%B0%20Apache%20%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84%E6%9C%AC%E5%9C%B0%E4%B8%BB%E6%9C%BA%E6%97%B6%EF%BC%8C%E4%BD%BF%E7%94%A8%E9%87%8D%E5%86%99%E8%A7%84%E5%88%99%E9%87%8D%E5%AE%9A%E5%90%91%E7%94%A8%E6%88%B7.png)
我正在使用 运行 Apache 2.4 服务器docker-compose
。
我有一个“主”虚拟主机,其中包括以子域开头的重写/test/
路径test
:
<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteRule "^/test/(.*)$" "http://test.example.com/$1" [P]
</VirtualHost>
子域test
不存在,无法创建(出于与问题无关的业务原因)。我完成解决的方式test.example.com
是在以下位置包含主机条目docker-compose.yml
:
version: "3.7"
services:
test:
image: httpd:2.4
ports:
- 80:80
extra_hosts:
- "test.example.com:127.0.0.1"
这是可行的,但是在虚拟主机中test.example.com
我有另一条重写规则:
<VirtualHost *:80>
ServerName test.example.com
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/login
RewriteCond %{HTTP:COOKIE} ^.*cookie=([^;]+)
RewriteRule /login /dashboard [R=302]
</VirtualHost>
该规则在成功登录时正确重写http://test.example.com/login
为http://test.example.com/dashboard
,但随后重定向发生在用户的浏览器中,http://test.example.com
无法解析(ERR_NAME_NOT_RESOLVED
)。
我如何在test.example.com
vhost 中执行此重写以便它正确地将用户重定向到该/dashboard
页面?
答案1
您可以将重定向添加到 example.com 的配置中:
<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP:COOKIE} ^.*cookie=([^;]+)
RewriteRule "^/test/login/?$" "/test/dashboard" [R=302]
RewriteRule "^/test/(.*)" "http://test.example.com/$1" [P]
</VirtualHost>