Apache重写不按预期重写

我试图使用mod_rewrite将用户redirect到HTTPS等价的URL,除非URL是用于我的Jenkins CI服务器。 这是我在我的默认站点configuration(我的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 ,我被http://qa.example.com/jenkins/https://qa.example.com/ ,但是如果我浏览到http://qa.example.com/jenkins/然后我的jenkinsconfiguration踢。后者的作品,但前者不,即浏览到http://qa.example.com/不会重新指引我https://qa.example.com/

不要将这些规则放在_default_虚拟主机中,而是将它们放在/etc/apache2/sites-enabled/jenkins虚拟主机中,这样它就可以对映射到该名称的请求起作用。

另外 – 你的第二个RewriteRule的expression式永远不会匹配/jenkins/ ,因为它至less需要一个字符在前导斜杠之前。 尝试这个:

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