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'm using the following .htaccess to redirect example.com or www.example.com to http://www.example.com.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

If the address is example.com/login I would to redirect to https instead of http. How to do that?

Thank you.

share|improve this question

3 Answers 3

 RewriteRule   ^login  https://%{HTTP_HOST}%{REQUEST_URI}  [R,L]
share|improve this answer
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# check if https if off and check if the requested uri ends with login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} login$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
share|improve this answer
    
I tried your example and it's not forwarding to https. If I enter "example.com/login" it redirects to "example.com";. Do I miss something? –  Tom Feb 28 '13 at 17:45
    
You can try replacing: RewriteCond %{HTTPS} off by this: RewriteCond %{SERVER_PORT} ^80$ –  Jose Robinson Mar 1 '13 at 20:19
    
Tried it but it's also not redirecting to https. What else could I try? –  Tom Mar 2 '13 at 21:49
    
Are you testing on local? Did you do a test on a remote server (ie: a hosting server)? I'm asking you because I believe that you have a configuration issue. –  Jose Robinson Mar 9 '13 at 14:02
    
I'm testing it on a hosting server. What kind of configuration issue could it be? –  Tom Mar 21 '13 at 10:23

You can force the user to browse your website through a Secured Sockets Layer (SSL) by writing these lines in your .htaccess file.

Engine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://www.website.com/$1 [R]

This being said, you will have to create some certificates, and make some other manipulations to handle it, otherwise each time you will try to connect, you will have an ugly 107 error (net::ERR_SSL_PROTOCOL_ERROR) : Error with SSL protocol. Just search on Google "how to make server https", and there is a bunch of relevant results.

share|improve this answer
    
Can you please improve the answer by adding the line for /login - in the question. –  Akber Choudhry Mar 4 '13 at 20:20
    
@akber-choudhry It actually works with any adress the user may request, which includes /login. –  chlkbumper Mar 5 '13 at 1:11

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.