0

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.

3 Answers 3

0
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]
5
  • 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, 2013 at 17:45
  • You can try replacing: RewriteCond %{HTTPS} off by this: RewriteCond %{SERVER_PORT} ^80$ Mar 1, 2013 at 20:19
  • Tried it but it's also not redirecting to https. What else could I try?
    – Tom
    Mar 2, 2013 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. Mar 9, 2013 at 14:02
  • I'm testing it on a hosting server. What kind of configuration issue could it be?
    – Tom
    Mar 21, 2013 at 10:23
0
 RewriteRule   ^login  https://%{HTTP_HOST}%{REQUEST_URI}  [R,L]
0

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.

2
  • Can you please improve the answer by adding the line for /login - in the question. Mar 4, 2013 at 20:20
  • @akber-choudhry It actually works with any adress the user may request, which includes /login.
    – mrcendre
    Mar 5, 2013 at 1:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .