So I want to redirect a page in a directory, for example auth/login.php
, to another page, at auth/
. The page is actually called auth/index.php
but I don't want this to show up to the client. How can I do this using this PHP code:
header("Location: /auth/");
However, I want the filename to be relative, e.g. ../
instead of hard-coded as auth
. Is this possible, and how?
To direct the users to your current directory, you should extract the script's directory from $_SERVER['SCRIPT_NAME']
, and then compare it to the actual HTTP request ($_SERVER['REQUEST_URI']
). If they're not the same - redirect to the current directory. If they're the same - you should start your actual script:
<?php
$currentScript = $_SERVER['SCRIPT_NAME'];
$pathInfo = pathinfo($currentScript);
$currentDir = $pathInfo['dirname'].'/';
if ($currentDir != $_SERVER['REQUEST_URI'])
{
header('Location: '.$currentDir);
return ;
}
// Here be real site data