Apache Rewrite 未按预期进行重写

Apache Rewrite 未按预期进行重写

我正在尝试使用 mod_rewrite 将用户重定向到 HTTPS 等效 URL,除非该 URL 是我的 Jenkins CI 服务器。这是我的默认站点配置中的规则(我的 Jenkins 和其他站点有各自的VirtualHost条目:

<VirtualHost _default_:80>
    ServerAdmin [email protected]
    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    ReWriteCond %{REQUEST_URI} !^.+/jenkins/(.*)$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

任何帮助将不胜感激。

编辑:根据要求,以下是输出apache2ctl -S

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443                  is a NameVirtualHost
         default server ip-10-72-226-167.ec2.internal (/etc/apache2/sites-enabled/default-ssl:2)
         port 443 namevhost ip-10-72-226-167.ec2.internal (/etc/apache2/sites-enabled/default-ssl:2)
         port 443 namevhost qa.example.com (/etc/apache2/sites-enabled/example-ssl:2)
*:80                   is a NameVirtualHost
         default server ip-10-72-226-167.ec2.internal (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost ip-10-72-226-167.ec2.internal (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost qa.example.com (/etc/apache2/sites-enabled/jenkins:1)
Syntax OK

我期望的是,当我浏览到 时,http://qa.example.com我会被重定向到https://qa.example.com/,但如果我浏览到 ,http://qa.example.com/jenkins/那么我的 Jenkins 配置就会启动。后者可以工作,但前者不行,即浏览到http://qa.example.com/不会将我重定向到https://qa.example.com/

答案1

不要将这些规则放在_default_vhost 中,而是将它们放在/etc/apache2/sites-enabled/jenkinsvhost 中,以便它可以针对已映射到该名称的请求起作用。

另外 - 第二个表达式RewriteRule永远不会匹配/jenkins/,因为它要求在前导斜杠前至少有一个字符。试试这个:

ReWriteCond %{SERVER_PORT} !^443$
ReWriteCond %{REQUEST_URI} !^/jenkins/.*$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]

相关内容