Using PHP to Redirect HTTP Traffic to HTTPS

Redirect http to https in PHP

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['HTTP_X_FORWARDED_PROTO'] or $_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.

Read Also  Researchers Find Severe Flaw in NextGEN Gallery WordPress Plugin