Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

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
        </Directory>
</VirtualHost>

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

.htaccess:

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]

Test:

http://localhost/admin->https://localhost/admin
https://localhost/admin don't redirect
https://localhost/test->http://localhost/test

Tesing:

# wget --no-check-certificate https://localhost/dgfjt943jreiorgd
--2011-01-31 16:55:18--  https://localhost/dgfjt943jreiorgd
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:443... connected.
WARNING: certificate common name `xxx.com' 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|127.0.0.1|: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
add comment

Your Answer

 
discard

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.