Cody Raspien Cody Raspien - 1 year ago 107
PHP Question

php - header location - wrong url opening


In index.php, I have the following code:

header('Location: page2.php');

However, instead of opening page2.php, the following URL is opened:

If I put an absolute URL, everything works.

  1. Why is this happening?

  2. Is there any workaround so that I don't have to use an absolute URL?

Answer Source
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download