Search code examples
phphtmlsessionslash

Double Slash at end of URL when going to HTTPS?


My site currently uses http and https sections based on the data being collected on the site (form data uses https).

On my index page, I have the PHP code at the top:

<?php
    session_start();
    ob_start();
    if( $_SERVER['SERVER_PORT'] == 443) {
        header('Location:http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])); 
        die();
    } 
?>

However, the page will not load and I get a 404 error. Similarly, when i visit the sections with https security using the head code:

<?php
session_start();
ob_start();
    if( $_SERVER['SERVER_PORT'] == 80) {
        header('Location:https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.basename($_SERVER['PHP_SELF'])); 
        die();
    }
?>

The site does not respond AND for some reason creates a double slash when switching from http to https.

Example: http://www.abc.com/, then clicking button which should route to enroll.php shows http://www.abc.com//enroll.php

why the need for the double slash and can anybody help with the 404 errors?


Solution

  • dirname() won't work on PHP_SELF because that is not necessarily a full directory.

    dirname("/enroll.php") will correctly return an empty string, which in turn leads to the double //.

    What exactly are you trying to do?