Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog.

php force redirect from http to https

Here is a quick php script that will force any http connections to https. This assumes that SSL is working properly on your server.

if ($_SERVER['SERVER_PORT']!=443)
{
	$url = "https://". $_SERVER['SERVER_NAME'] . ":443".$_SERVER['REQUEST_URI'];
	header("Location: $url");
}

3 Responses to “php force redirect from http to https”

  1. Nathan Wallwork says:

    Should also call exit, or the rest of the script will still run,and may leak information over http.

  2. phpnoob says:

    how do you force https://www. ???

    One of my sites only allows for the http://www.domain.com to access ssl, so any other format ends with an error.

    domain.com -> bad
    http://domain.com -> bad
    http://www.domain.com -> good

  3. admin says:

    Nathan – thanks. I had no idea that the rest of the script would continue to run.

    phpboon – I think you would just add in the www right before calling the global variable $_SERVER['SERVER_NAME']

Leave a Reply