Skip to content

Instantly share code, notes, and snippets.

@iloris
Created April 22, 2013 22:19
Show Gist options
  • Save iloris/5439056 to your computer and use it in GitHub Desktop.
Save iloris/5439056 to your computer and use it in GitHub Desktop.
PHP SSL redirection
<?php
function is_ssl() {
if ( isset($_SERVER['HTTPS']) ) {
if ( 'on' == strtolower($_SERVER['HTTPS']) )
return true;
if ( '1' == $_SERVER['HTTPS'] )
return true;
} elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
return true;
}
if ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' ) {
return true;
}
return false;
}
@iloris
Copy link
Author

iloris commented Apr 22, 2013

use like this :

    if( !is_ssl() ){
    $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    header("Location:$redirect");
    }

@msurguy
Copy link

msurguy commented Apr 22, 2013

Just curious, what is the advantage of using this over redirecting the whole site to HTTPS?

@iloris
Copy link
Author

iloris commented Apr 23, 2013

You can use this on the whole site to https but the advantage is that it's easier to configure than htaccess, if someone visit the website without https, he will be redirected to the https version, you don't need to hardcode the domain name in the htaccess so it's also easier for locahost testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment