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

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.
Programming

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");
}

Related posts:

  1. php/linux – performing an http post the easy way
  2. setting up ssl for apache2 on debian linux

Tags: ,

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