• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
http to https using .htaccess

I was bogged down with this problem a couple of weeks ago, I had to rewrite a couple of urls to https. The rest of the url should be on http, that means if they are on https they should be written to http.

I couldnt get the solutions easily, so I m posting it here.

first of all you need to change your base url to

$config['base_url'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') ? 'https://' : 'http://' ;
$config['base_url'] .= $_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME'])).'/';

with this for a https page all the links created with base_url() will automatically be https.


Secondly, you need the following entry to ur .htaccess file, tweak it as per the requirement

RewriteCond %{HTTPS} !on
RewriteRule ^(.*)/(view|checkout|pay)(.*)$ https://%{HTTP_HOST}/cart/$2$3 [R=301,NC,L]

RewriteCond %{HTTPS} on
RewriteRule ^(.*)/(success)(.*)$ http://%{HTTP_HOST}/cart/$2$3 [R=301,NC,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_REFERER} !^https(.*)/(view|checkout|pay)(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^(.*)/(view|checkout|pay)(.*)$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]

the condition RewriteCond %{HTTP_REFERER} !^https(.*)/(view|checkout|pay)(.*)$ [NC] doesnt rewrite the links to http which are on a https page.

you can go through this stackoverflow link for a little more details

Hope this helps someone ..


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.