I'm trying to use mod_rewrite to redirect users to the HTTPS equivalent URL unless the URL is for my Jenkins CI server. Here is the rule I have in my default site config (my Jenkins and other sites have their own respective VirtualHost
entries:
<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>
Any help would be greatly appreciated.
Edit: As requested, here is the output from 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
What I am expecting is that when I browse to http://qa.example.com
that I get re-directed to https://qa.example.com/
, but if I browse to http://qa.example.com/jenkins/
then my Jenkins config kicks in. The latter works, but the former does not, i.e. browsing to http://qa.example.com/
does not re-direct me to https://qa.example.com/
.
Instead of putting those rules in the
_default_
vhost, put them in the/etc/apache2/sites-enabled/jenkins
vhost, so that it can work against requests that have been mapped to that name.Also - the expression on your second
RewriteRule
won't ever match/jenkins/
, since it's requiring at least one character before the leading slash. Try this: