35

How can I redirect to the same page using PHP?

For example, locally my web address is:

http://localhost/myweb/index.php

How can I redirect within my website to another page, say:

header("Location: clients.php");

I know this might be wrong, but do I really need to put the whole thing? What if later it is not http://localhost/?

Is there a way to do something like this? Also, I have a lot of code and then at the end after it is done processing some code... I am attempting to redirect using that. Is that OK?

3

9 Answers 9

96

My preferred method for reloading the same page is $_SERVER['PHP_SELF']

header('Location: '.$_SERVER['PHP_SELF']);
die;

Don't forget to die or exit after your header();

Edit: (Thanks @RafaelBarros )

If the query string is also necessary, use

header('Location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
die;

Edit: (thanks @HugoDelsing)

When htaccess url manipulation is in play the value of $_SERVER['PHP_SELF'] may take you to the wrong place. In that case the correct url data will be in $_SERVER['REQUEST_URI'] for your redirect, which can look like Nabil's answer below:

header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;

You can also use $_SERVER[REQUEST_URI] to assign the correct value to $_SERVER['PHP_SELF'] if desired. This can help if you use a redirect function heavily and you don't want to change it. Just set the correct vale in your request handler like this:

$_SERVER['PHP_SELF'] = 'https://sample.com/controller/etc';
11
  • 7
    If the query string is also necessary, use header('location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); exit;. Apr 23, 2014 at 13:39
  • 2
    @RafaelBarros Thanks for that, I've edited my answer to include your suggestion. Apr 25, 2014 at 18:49
  • 4
    If you use .htaccess to redirect URL's to a single file, this will redirect to that single file and not the URL in the browser. Nov 2, 2017 at 8:19
  • I have problem with ampersand. Solution: header('Location:'. $_SERVER['PHP_SELF'].'?'. str_replace("&","&",$_SERVER['QUERY_STRING']));die;
    – houssam
    Jan 4, 2019 at 10:33
  • @HugoDelsing If you're redirecting to a request handler, you can set $_SERVER['PHP_SELF'] = 'whatever you want'; I usually do this in the request handler itself, with pieces parsed out from some manual config values and $_SERVER['REQUEST_URI'] (split between each / via regex). This lets me use my redirect code from above without worry and also gives me granular control over my friendly url format. So when htaccess url manipulation is in play use $_SERVER['REQUEST_URI'] which you can use to assign the correct value to $_SERVER['PHP_SELF'] if desired. Jan 4, 2019 at 16:21
18

Another elegant one is

header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;
0
12

There are a number of different $_SERVER (docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']:

header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);

where $location is the path after the domain, starting with /.

2
  • I i am not sure why it is not redirecting, maybe i did it wrong? $location= '/index.php'; header("Location: " . "http://" . $_SERVER['HTTP_HOST'] .$location);
    – user710502
    Nov 15, 2011 at 3:21
  • 1
    For debugging, simply output that string with echo instead of header. If it is correct, then something else is wrong (headers must be the first output on the page, for example.)
    – Nicole
    Nov 15, 2011 at 3:24
10

To really be universal, I'm using this:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' 
    || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: '.$protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;

I like $_SERVER['REQUEST_URI'] because it respects mod_rewrite and/or any GET variables.

https detection from https://stackoverflow.com/a/2886224/947370

2
  • This one is helpful.
    – Amir Iqbal
    Jan 9, 2019 at 19:33
  • ... i mean, in 2022 I think we should probably all be redirecting exclusively to https in almost all circumstances - but I'll leave this answer as is for now. Feb 22, 2022 at 23:19
5

A quick easy approach if you are not concerned about query params:

header("location: ./");
2
header('Location: '.$_SERVER['PHP_SELF']);  

will also work

2

Simple line below works just fine:

header("Location: ?");
1

I just tried using header("Location: "); (without any value) and it redirected to the current page.

1
  • Providing a destination is old school as a blank url in Internet Explorer would redirect the user to the root of the current folder like "Location: ./". Modern webkit browsers does not.
    – tim
    Feb 22, 2022 at 22:16
-2

I use correctly in localhost:

header('0');
1
  • This does not reload the page.
    – rybo111
    Aug 8, 2016 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.