HTTP to HTTPS php redirect?

2 replies
Hi guys,

I am trying to get a page on my site to redirect to an https site. My site is an http.

I tried this code:
PHP Code:
<?php
if ($_SERVER['SERVER_PORT']!=443)
{
$url "https://www.securesite.com"$_SERVER['SERVER_NAME'] . ":443".$_SERVER['REQUEST_URI'];
header("Location: $url");
}
?>
It redirects but it adds this to the end of the url: mysite.com:443/redirect.php

so after it has redirected, in the browser it looks like this
PHP Code:
https://thesecuresite.com:443/redirect.php 
So it gets a 404 error on the https site.

I tried another one I found but it did the same thing, without the 443. Just he mysite.com/redirect.php on the end.

Any thoughts?
#http #https #php #redirect
  • Profile picture of the author Ashley Gable
    Actually the end URL looks like this:

    PHP Code:
    https://thesecuresite.commysite.com:443/redirect.php 
    sorry accidentally left mysite.com out
    {{ DiscussionBoard.errors[1464987].message }}
  • Profile picture of the author m4rx
    You need to remove $_SERVER['REQUEST_URI'] from the $url var. The reason why you are getting the weird ending is because you are using the request uri as the end of $url, which is /redirect.php

    --m4rx
    Signature
    We are what we repeatedly do. Excellence, then, is not an act, but a HABIT. ~Aristotle
    Bored. Learn everything you need to know about Organic Gardening.
    {{ DiscussionBoard.errors[1465176].message }}

Trending Topics