Haradzieniec
Haradzieniec

Reputation: 9340

.htaccess: redirect http to https or https to http depending on URL

The solution below covers required rules:

1) http://www.mydomain.com , http://www.mydomain.com/?p=home , http://www.mydomain.com/?p=home1 , http://www.mydomain.com/?qqq=home are always http, even if https are typed instead of http;

2) all the rest pages are always https, even if http was typed instead of https;

but in practice does NOT cover

3) //www.mydomain.com/administration/any_file_in_admin_folder.php should always be https as well (even with parameter ?p=home etc).

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]


#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to implement 3) to this code so it works? (I still have no luck and need help). Thank you.

Upvotes: 0

Views: 2063

Answers (3)

ThinkingMonkey
ThinkingMonkey

Reputation: 12727

Try this: Add the first line in 2nd & 3rd block.

i.e. RewriteCond %{REQUEST_URI} !/administration/ [NC]

RewriteEngine on
RewriteBase /

#determine if page is supposed to be http
#Requested URi must not have administration in it
RewriteCond %{REQUEST_URI} !/administration/ [NC]
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]  [L]

#all pages that are supposed to be http redirected if https
RewriteCond %{REQUEST_URI} !/administration/ [NC]
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 2

Dave Child
Dave Child

Reputation: 7881

The second part of your rule, RewriteCond %{QUERY_STRING} ^$ matches any URL without a querystring. Your URL, //www.mydomain.com/administration/any_file_in_admin_folder.php has no querystring. So IS_HTTP is being set to 1, and your user is being redirected to HTTP.

Try this. It's untested - but basically you're identifying the "home" querystring first, and then handling http://www.mydomain.com separately.

#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

RewriteRule ^$ - [E=IS_HTTP:1]

This might also do the trick:

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{REQUEST_URI} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

Upvotes: 1

anubhava
anubhava

Reputation: 785108

Change your first rule to:

#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#or if request URI is: /administration/any_file_in_admin_folder\.php
RewriteCond %{REQUEST_URI} !^/*administration/any_file_in_admin_folder\.php$ [NC]
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]

Upvotes: 1

Related Questions