You can do this (relative addressing):
header("location: ../dashboard.php"); // for one folder
header("location: ../../dashboard.php"); // for two folders
Answer from Saqib Amin on Stack OverflowYou can do this (relative addressing):
header("location: ../dashboard.php"); // for one folder
header("location: ../../dashboard.php"); // for two folders
Get the server name using superglobal $_SERVER and add rest of the path.
$path = $_SERVER['SERVER_NAME'].'/the path';
header("location: " . $path ."/dashboard.php");
because you enclose it in double quotes below is correct as well
header("location: $path/dashboard.php");
PHP How to header-location to a page in a parent directory? - Stack Overflow
Path to folder - PHP - SitePoint Forums | Web Development & Design Community
http redirect - PHP header(Location: ...): Force URL change in address bar - Stack Overflow
php - Header Location relative path compatibility - Stack Overflow
have a try with this:
header("Location: ../first.php")
or use an absolute path or url instead.
Explanation: .. is the unix/linux expression used for 'parent directory'. The Internet is unix land, so that rules applies there too.
Instead of:
header("Location: ../first.php")
try with this:
header("url: ../first.php")
this will change the url.Using location don't work in some cases
Since: '../' is to denote 'parent directory'.So,this will work.
Try changing:
header("Location : blabla")
^
|
(whitespace)
To
header("Location: blabla")
Well, if the server sends a correct redirection header, the browser redirects and therefore "changes the url". It might be a browser issue, then. I don't know if it has anything to do with it, but you should not send a relative url in the location header ("HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. ", http://php.net/manual/en/function.header.php), and "location" must be capitalized, like:
header('Location: http://myhost.com/mypage.php');
You should really use absolute paths (at least relative to your document root).
Consider if you move checkLogin.php a directory deeper…
Any of the follow won't have a problem.
header("Location: /landing.html");
// or
header("Location: http://www.example.com/landing.html");
The accepted answer will take you to the root of the domain, not 1 folder up as specified.
To go one folder up:
header("Location: ../landing.html");
header("Location: http://".$_SERVER['HTTP_HOST'].$formaction;
location should contain URL, not server file path
You could theoretically create a PHP File at the Root of your Application and call it anything you like: say
redirect.php. Then inside of thisredirect.php, you could put some code that might look something like this:
<?php
// CREATE AN ARRAY THAT WITH NUMERIC KEY
// THAT WILL CORRESPOND TO THE FILE YOU WISH TO REDIRECT TO (ACTUALLY LOAD)
// SINCE THIS IS A PSEUDO REDIRECT:
$fileMap = array(
0 => "./file-1.php",
1 => "./file-2.php",
2 => "./file-3.php",
3 => "./file-4.php",
4 => "./file-5.php",
5 => "./file-6.php",
6 => "/cert/forms/sessions/session-inc-info.php",
7 => "./../../session-inc-info.php", //<== YOU MAY KEEP ADDING TO LIST
);
// CHECK THAT THERE IS A GET PARAM WITH NAME: to
if(isset($_GET['to'])){
$fileKey = $_GET['to']; //<== GET THE `to` PARAM FROM $_GET
$loadableFile = $fileMap[$fileKey]; //<== TRANSLATE `to` KEY TO A LOADABLE FILE
// IF THE FILE [$loadableFile] EXISTS, LOAD IT
// OTHERWISE REDIRECT TO MAIN-APPLICATION PAGE (HOMEPAGE)
if(file_exists($loadableFile)){
require_once $loadableFile;
}else{
header("location: index.php"); //<== ASSUMES index.php IS THE MAIN FILE.
exit;
}
}
// IF ALL FAILS, STILL REDIRECT TO THE MAIN-APPLICATION PAGE
header("location: index.php"); //<== ASSUMES index.php IS THE MAIN FILE.
Then on each page where you wish to Perform a redirect, you may simply do something like this:
<?php
// ASSUMING YOU WANT TO RE-DIRECT TO: `./../../session-inc-info.php`
// AS YOU CAN SEE FROM THE `$fileMap` ARRAY, `./../../session-inc-info.php`
// HAS THE KEY OF "7"
// NOW ALL YOU CAN DO IS SIMPY APPEND "?to=7" TO `redirect.php`
// IN THE `header()` METHOD LIKE THIS:
header("location: redirect.php?to=7");
exit;
/*EXPECTED TO LOAD THE FILE: `./../../session-inc-info.php`
NOTE:
This is not really a Redirect (as it is basically just loading[including] Files from a given Location/Path, however since it seems you are trying to load some file like:/cert/forms/sessions/session-inc-info.php, it is assumed that this may be an option. To do a real Redirect: just use the header() Function directly passing it the exact URL-Location to which to redirect.
Cheers and Good-Luck...
I am attempting to redirect the user to index.html. If I use header("location: index.html");, it will show localhost/site/index.html instead of localhost/site/. However, if I use "location: ." (location: [dot]), then it does not show index.html in the url.
What does the dot mean?