{"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

$_SERVER['HTTP_X_FORWARDED_PROTO']<\/code><\/pre>\n\n\n\n

or <\/p>\n\n\n\n

$_SERVER['HTTPS']<\/code><\/pre>\n\n\n\n

variables exist or checking for a specific value of those variables.<\/p>\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

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":"\nHow to Redirect HTTP to HTTPS Traffic Using PHP<\/title>\n<meta name=\"description\" content=\"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Redirect HTTP to HTTPS Traffic Using PHP\" \/>\n<meta property=\"og:description\" content=\"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-01T08:31:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-08T15:48:11+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"Using PHP to Redirect HTTP Traffic to HTTPS\",\"datePublished\":\"2019-11-01T08:31:06+00:00\",\"dateModified\":\"2021-12-08T15:48:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\"},\"wordCount\":129,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg\",\"keywords\":[\"HTTP\",\"Internet\",\"Open Source\",\"Optimization\",\"Performance\",\"Programming\",\"Security\",\"Software Design\",\"Software development\",\"Web\",\"Web Applications\",\"Web Development\",\"Websites\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Technology\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\",\"url\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\",\"name\":\"How to Redirect HTTP to HTTPS Traffic Using PHP\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg\",\"datePublished\":\"2019-11-01T08:31:06+00:00\",\"dateModified\":\"2021-12-08T15:48:11+00:00\",\"description\":\"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/redirect-http-https-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Redirect http to https in PHP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/redirect-http-https-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using PHP to Redirect HTTP Traffic to HTTPS\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Redirect HTTP to HTTPS Traffic Using PHP","description":"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/redirect-http-https-php\/","og_locale":"en_US","og_type":"article","og_title":"How to Redirect HTTP to HTTPS Traffic Using PHP","og_description":"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files","og_url":"http:\/\/local.brightwhiz\/redirect-http-https-php\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2019-11-01T08:31:06+00:00","article_modified_time":"2021-12-08T15:48:11+00:00","og_image":[{"width":1200,"height":630,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"Using PHP to Redirect HTTP Traffic to HTTPS","datePublished":"2019-11-01T08:31:06+00:00","dateModified":"2021-12-08T15:48:11+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/"},"wordCount":129,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg","keywords":["HTTP","Internet","Open Source","Optimization","Performance","Programming","Security","Software Design","Software development","Web","Web Applications","Web Development","Websites"],"articleSection":["Articles","Guides","How To","Technology"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/","url":"http:\/\/local.brightwhiz\/redirect-http-https-php\/","name":"How to Redirect HTTP to HTTPS Traffic Using PHP","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg","datePublished":"2019-11-01T08:31:06+00:00","dateModified":"2021-12-08T15:48:11+00:00","description":"Here's how to redirect http traffic to https using PHP functions if you do not have access to the web server htaccess files","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/redirect-http-https-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2019\/10\/redirect-http-php.jpg","width":1200,"height":630,"caption":"Redirect http to https in PHP"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/redirect-http-https-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"Using PHP to Redirect HTTP Traffic to HTTPS"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/8117"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=8117"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/8117\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/8119"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=8117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=8117"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=8117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}