Add Forfully Redirect http to https using php code without .htaccess

This tutorial for redirect the http url to https using the php code if your .htaccess will not work for the SSL. it will just redirect all your http url to https without using the .htaccess if your server https is not on. just follow the step and you can get your own redirction.

Step 1)

just put this code in your main root file or connection file which is included in everywhere of your project. so everytime url will scan with this code and if it finds the http then convert into https.

if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || 
   $_SERVER['HTTPS'] == 1) ||  
   isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&   
   $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
   $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   exit();
}

One thought on “Add Forfully Redirect http to https using php code without .htaccess

Leave a Reply

Your email address will not be published. Required fields are marked *