Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>
    RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    ReWriteCond %{REQUEST_URI} !^.+/jenkins/(.*)$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

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 (/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 (/etc/apache2/sites-enabled/jenkins:1)
Syntax OK

What I am expecting is that when I browse to that I get re-directed to, but if I browse to then my Jenkins config kicks in. The latter works, but the former does not, i.e. browsing to does not re-direct me to

share|improve this question
Please provide the output from apache2ctl -S. What behavior do you see instead of what's expected? – Shane Madden May 16 '12 at 3:27
Content added. Thanks Shane. – John S May 16 '12 at 4:03
up vote 1 down vote accepted

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:

ReWriteCond %{SERVER_PORT} !^443$
ReWriteCond %{REQUEST_URI} !^/jenkins/.*$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
share|improve this answer
Worked like a charm, thanks Shane! – John S May 16 '12 at 11:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.