We published a guide showing how to force HTTPS using the Apache .htaccess configuration file. That is a system that works but is restricted to those using Apache as a web server. In today’s guide, we will show you how to use PHP to redirect HTTP to HTTPS.
The PHP solution provided here will work with different web servers including the mainstream one such as Apache, Nginx, and IIS.
Like all redirects, bad configurations can cause endless redirect loop errors. This involves checking if
$_SERVER['HTTPS'] variables exist or checking for a specific value of those variables.
The Working PHP Redirect HTTP to HTTPS Code
There you have it, that is how you can use PHP to redirect HTTP to HTTPS.