HTTP to HTTPS redirect?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rockybloggs
Forum Newbie
Posts: 3
Joined: Mon Dec 07, 2009 1:14 pm

HTTP to HTTPS redirect?

Post by rockybloggs »

Hi everyone, this is my first post. I am having trouble with a redirect I am trying to put on an a page of mine. I need to redirect visitors from my site, which is an http site, to an https page on another site.
I can get it to redirect to the desired https page, but it appends this on the the end of the url:
  • myhttpsite.com:443
Here is the code I am using.

Code: Select all

<?php 
if ($_SERVER['SERVER_PORT']!=443)
{
$url = "https://securesite.com". $_SERVER['SERVER_NAME'] . ":443";
header("Location: $url");
}
?>
So when it redirects the URL looks like this:
  • https://securesite.commyhttpsite.com:443
Can anyone help me? I have just started to get into php, like a week ago, and I thought I could do this as it would be easy!
Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: HTTP to HTTPS redirect?

Post by AbraCadaver »

Where do you want to redirect?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rockybloggs
Forum Newbie
Posts: 3
Joined: Mon Dec 07, 2009 1:14 pm

Re: HTTP to HTTPS redirect?

Post by rockybloggs »

AbraCadaver wrote:Where do you want to redirect?
Its to a secure order page. I took out the original url in the code, I replaced it with securesite.com.

Code: Select all

<?php
   if ($_SERVER['SERVER_PORT']!=443)
   {
   $url = "https://securesite.com". $_SERVER['SERVER_NAME'] . ":443";
   header("Location: $url");
   }
   ?>
Rocky
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: HTTP to HTTPS redirect?

Post by AbraCadaver »

rockybloggs wrote:
AbraCadaver wrote:Where do you want to redirect?
Its to a secure order page. I took out the original url in the code, I replaced it with securesite.com.

Code: Select all

<?php
   if ($_SERVER['SERVER_PORT']!=443)
   {
   $url = "https://securesite.com". $_SERVER['SERVER_NAME'] . ":443";
   header("Location: $url");
   }
   ?>
Rocky
Yeah I got that, but what's with the $_SERVER['SERVER_NAME']?

Why not just:

Code: Select all

$url = "https://securesite.com";
header("Location: $url");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: HTTP to HTTPS redirect?

Post by pickle »

You don't need to specify port 443. HTTPS traffic is automatically (by default) served over port 443, just like plain HTTP traffic is served over port 80.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rockybloggs
Forum Newbie
Posts: 3
Joined: Mon Dec 07, 2009 1:14 pm

Re: HTTP to HTTPS redirect?

Post by rockybloggs »

Yeah I got that, but what's with the $_SERVER['SERVER_NAME']?

Why not just:

Code: Select all

$url = "https://securesite.com";
header("Location: $url");
Thanks that did it! I dont know why I thought that, I just figured it would need it. It was in the others I found around the internet for https redirects.

Thanks again!
Post Reply