Dan
Dan

Reputation: 12096

.htaccess force redirect a rewritten url in htaccess to use https ssl

I want to force SSL for some of my rewritten pages so that they must use SSL.

The urls are:

http://domain.com/signin
http://domain.com/register

I want them to be:

https://domain.com/signin
https://domain.com/register


This is my current .htaccess:

RewriteEngine on
RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC] 


I have tried the following but it was not successful:

RewriteEngine on
RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC] 

RewriteCond     %{SERVER_PORT} !443
RewriteCond     %{REQUEST_URI} ^/(signin|register)
RewriteRule     (.*) https://domain.com/$1 [R]

Upvotes: 1

Views: 1278

Answers (2)

anubhava
anubhava

Reputation: 786091

You need redirect to https before internal rewrites.

Use this code:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(signin|register)/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

RewriteCond %{HTTPS} on
RewriteRule !^(signin|register)/? http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]

RewriteRule ^signin$       index.php?&action=showLogin [QSA,L,NC]
RewriteRule ^register$     index.php?&action=showRegister [QSA,L,NC]

Upvotes: 1

user2779846
user2779846

Reputation: 13

http://www.besthostratings.com/articles/force-ssl-htaccess.html

Sometimes you may need to make sure that the user is browsing your site over securte connection. An easy to way to always redirect the user to secure connection (https://) can be accomplished with a .htaccess file containing the following lines:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Please, note that the .htaccess should be located in the web site main folder.

In case you wish to force HTTPS for a particular folder you can use:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
The .htaccess file should be placed in the folder where you need to force HTTPS.

Upvotes: 0

Related Questions