by
2
6
2,482
1
Top 1% !
Popular
Famous
Tagged
Specified
OpenSource
Popularity: 6763rd place

Published on:

Extensionphp
LanguagePHP
LicenseMIT_X11

PHP 301 redirect non-HTTPS requests to https://

Redirects based on port and protocol
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/415536/PHP-301-redirect-non-HTTPS-requests-to-h?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
<?php if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') { if(!headers_sent()) { header("Status: 301 Moved Permanently"); header(sprintf( 'Location: https://%s%s', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] )); exit(); } } ?> <?php if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){ $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("HTTP/1.1 301 Moved Permanently") header("Location: $redirect"); } ?> <?php if ($_SERVER["SERVER_PORT"] != 443) { $redir = "Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header($redir); header("HTTP/1.1 301 Moved Permanently"); exit(); } ?>
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus