htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed


Could anybody please help me to write rewriterule, that redirects http<->https (back and force depending of URL typed) with these conditions:

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.

The code below redirects all urls (and keeps parameters), except <a href="http://www.mydomain.com" rel="nofollow">http://www.mydomain.com</a> , to https.

#redirects http to https if there are parameters RewriteCond %{HTTPS} off RewriteCond %{QUERY_STRING} !^$ RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

I've tried to add the code below right after the code above to redirect https to http as well (if there are no parametres), so that all pages are always https, except www.mydomain.com, but I had no luck. Also I missed ?p=home, ?p=home1, ?qqq=home - I don't know how to add them.

RewriteCond %{HTTP} off RewriteCond %{QUERY_STRING} ^$ RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}


Try adding the following to your htaccess file in the root directory of your site.

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,L=301] #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,L=301]


  • Awk script not working when passed to SSH in a heredoc [duplicate]
  • MySQL count on precondition?
  • Creating nested list using JSON and jquery
  • Storing Variable Row/Column CSV Files In Database
  • SilverStripe. How to redirect from www.domainName to domainName?
  • Nginx custom PHP error_pages with different header status codes and body contents
  • How can I .htaccess 301 redirect all pages/files (except for one) to new domain?
  • Django how to modify database records by template
  • Can I put a + sign in a folder with IIS?
  • Open hyperlink on click on an ggplot/plotly chart
  • Redirect to trailng slash (htaccess)
  • Symfony2 redirect to https route fails (uses wrong port)
  • WordPress > setting permalink option via script buggy?
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • JqueryMobile Popup menu is not working
  • Authentication in Play! and RestEasy
  • jQuery ready not fired after rails link_to is clicked
  • Bad request using file_get_contents for PUT request in PHP
  • Repeat a vertical line on every page in Report Builder / SSRS
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • RectangularRangeIndicator format like triangular using dojo
  • Cross-Platform Protobuf Serialization
  • JSON with duplicate key names losing information when parsed
  • php design question - will a Helper help here?
  • Matrix multiplication with MKL
  • Revoking OAuth Access Token Results in 404 Not Found
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal