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've almost figured it out on my own but there's something I'm missing. I want to set a couple of directories and files to require SSL and everything else that's not related to those files and dirs to point back to http.

So far I have this:

RewriteEngine on
RewriteBase /
# Force ssl for login & admin
RewriteCond %{HTTPS} !on
RewriteRule ^/?(admin(.*)|login\.php)$ https://%{SERVER_NAME}/$1 [R,NC,L]

# Force non-ssl for others 
RewriteCond %{HTTPS} on
RewriteRule ^/?(admin(.*)|login\.php)$ http://%{SERVER_NAME}/$1 [R,NC,L]

I'm sure I'm doing something wrong but I just can't figure it out.... The first condition works perfect - whenever I access login.php or /admin/ it points to https. But the second one doesn't...

Where have I mistaken ?

Thanks in advance!

share|improve this question
up vote 2 down vote accepted

Likely to:

RewriteRule !^/?(admin(.*)|login\.php)$ http://%{SERVER_NAME}/$1 [R,NC,L]

My working config:

<VirtualHost *:80>
        DocumentRoot /var/www
        <Directory /var/www/>
                AllowOverride All
                Order allow,deny
                allow from all

<VirtualHost *:443>
        DocumentRoot /var/www
        <Directory /var/www/>
                AllowOverride All
                Order allow,deny
                allow from all
        SSLEngine on
        SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
        SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key


RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^admin$ https://%{SERVER_NAME}/$1 [R,NC,L]

RewriteCond %{HTTPS} on
RewriteRule !^admin$ http://%{SERVER_NAME}/$1 [R,NC,L]


https://localhost/admin don't redirect


# wget --no-check-certificate https://localhost/dgfjt943jreiorgd
--2011-01-31 16:55:18--  https://localhost/dgfjt943jreiorgd
Resolving localhost...
Connecting to localhost||:443... connected.
WARNING: certificate common name `' doesn't match requested host name `localhost'.
HTTP request sent, awaiting response... 302 Found
Location: http://localhost/ [following]
--2011-01-31 16:55:18--  http://localhost/
Connecting to localhost||:80... connected.
HTTP request sent, awaiting response... 200 OK
share|improve this answer
The code seems perfectly logic but it's not working ? – tftd Jan 31 '11 at 14:22
I test it(Ubuntu 10.04,Apache2,wget) and it's working. – alvosu Jan 31 '11 at 14:45
Slackware 13.1x64 with apache 2.2.17 and when you accessing localhost/admin -> localhost/admin , but when you access localhost/somethingjusttobesure.html you stay on HTTPS ? – tftd Jan 31 '11 at 16:50
I add wget test... You can write me a mail – alvosu Jan 31 '11 at 16:57
I've got no idea how I manage to get it working ... But your code really works... I knew it was something I was doing not right. Thank you for your help and effort to solve my problem! :) – tftd Jan 31 '11 at 17:54

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.