{"id":8117,"date":"2019-11-01T04:31:06","date_gmt":"2019-11-01T08:31:06","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8117"},"modified":"2021-12-08T10:48:11","modified_gmt":"2021-12-08T15:48:11","slug":"redirect-http-https-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/redirect-http-https-php\/","title":{"rendered":"Using PHP to Redirect HTTP Traffic to HTTPS"},"content":{"rendered":"\n
We published a guide showing how to force HTTPS using the Apache .htaccess<\/a> 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<\/a> to redirect HTTP to HTTPS.<\/p>\n\n\n\n The PHP solution provided here will work with different web servers including the mainstream ones such as Apache, Nginx, and IIS.<\/p>\n\n\n\n Like all redirects, bad configurations can cause endless redirect loop errors. This involves checking if <\/p>\n\n\n\n or <\/p>\n\n\n\n variables exist or checking for a specific value of those variables.<\/p>\n\n\n\n There you have it, that is how you can use PHP to redirect HTTP to HTTPS.<\/p>\n","protected":false},"excerpt":{"rendered":" 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…<\/p>\n","protected":false},"author":1,"featured_media":8119,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,16],"tags":[304,320,424,433,449,471,526,543,544,635,636,638,643],"yoast_head":"\n$_SERVER['HTTP_X_FORWARDED_PROTO']<\/code><\/pre>\n\n\n\n
$_SERVER['HTTPS']<\/code><\/pre>\n\n\n\n
The Working PHP Redirect HTTP to HTTPS Code<\/h2>\n\n\n\n
if (\n $_SERVER['HTTP_HOST'] != 'localhost' && \/\/ optionally disable on localhost\n !( \n ( $_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 ) || \n ( isset($_SERVER['HTTP_X_FORWARDED_PROTO'] ) && \n $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' )\n ) \n )\n{\n $redirect = 'https:\/\/' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];\n header('HTTP\/1.1 301 Moved Permanently');\n header('Location: ' . $redirect);\n exit();\n}<\/code><\/pre>\n\n\n\n