53

atm I'm using the following four lines to redirect the user to another page on my website:

<?php
    header("Status: 301 Moved Permanently");
    header("Location: ./content/index.html");
    exit;
?>

but there is a problem with the use of HTTP query string variables like http://< url >?param=blah
they don't get appended to url understandably.

Is there a smart move for implementing this?

greetings

5
  • 1
    I believe that you should use a fully qualifiedly uri so try http://domain.tld/path?get=params instead of /path?get=params, otherwise you may have to create a landing page with the meta redirect element. Commented Jan 15, 2011 at 21:12
  • What is the problem exactly? There are several possible interpretations Commented Jan 15, 2011 at 21:14
  • I#m sorry this was a domain.tld/path?get=params Commented Jan 15, 2011 at 21:15
  • but i wrote "< domain >" system changed it automatically;) Commented Jan 15, 2011 at 21:15
  • I found I had to remove all of the indent spacing before header(... before it would work php.net/manual/en/function.header.php but it did work with a string of queries assembled from all of the htmlspecialchars($_GET['foo']...htmlspecialchars($_GET['moo'] etc and assembling the search queries into a single var $bar with ` . '&' . ` in between each of them so then header("Location: $bar "); works without htmlspecialchars() messing up the & Commented Apr 2, 2021 at 17:17

7 Answers 7

107
<?php
    header("Status: 301 Moved Permanently");
    header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
    exit;
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Is it safe? Do we need to encode the query string in any way?
To avoid the risk of XSS attacks it would be wise to sanitise $_SERVER['QUERY_STRING'] with htmlspecialchars()
@nfrost21, this unfortunately won't work if you have more than one param, as htmlspecialchars() encodes the & delimiter and breaks the params.
This code will add an extra ? if no query string is passed
as @jontro pointed out, you better use something like: header("Location:./content/index.html" . ($_GET ? "?" . $_SERVER['QUERY_STRING'] : "")); To avoid adding an extra ? if there is no query string.
|
18

To do the redirect and make sure an extra question mark does not show up when query string is not passed use the following

function preserve_qs() {
    if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
        return "";
    }
    return "?" . $_SERVER['QUERY_STRING'];
}
header("Status: 301 Moved Permanently");
header("Location: ./content/index.html" . preserve_qs());

Comments

10

I would like to add one more option...
Anyways - like others have said, using (.htaccess) mod_rewrite would probably be the best option.

However,
there surely can be many situations when you have to do it in PHP => you have 2 options:

  1. Append your redirection URI with $_SERVER['QUERY_STRING']
  2. Append your redirection URI with a query built on your own : http_build_query($_GET);

Option 2. - Advantages (+)

  • Encodes the parameters ( default by PHP_QUERY_RFC1738 )
  • you can easily add (or remove) some $_GET params like:
    $_GET['new_param']="new value"; (add)
    unset($_GET['param_to_remove']); (remove)
  • if environment (god knows why) does not provide a QUERY_STRING - you are probably still able to get the superglobal $_GET => environmentaly independent
  • http_build_query()'s 1st param can actually be ANY array or object so you can build a $_GET-like request from $_POST or $o = new YourObject(); if necessary
  • you can change argument separator if necessary

Option 2. - Dissadvantages (-)

  • this kind of building query might be redundant ("good-for-nothing"), just unnecessary...
  • if the query is big enaugh (maybe some attack?) it could have an affect on performace, because everytime there will be an array converted to a string & encoded

For more info see http://www.php.net/manual/en/function.http-build-query.php - a PHP manual's site about the http_build_query() function which Returns a URL-encoded string.

Comments

4

Using $_SERVER['QUERY_STRING'] and appending it to the end of your redirect might be what you're looking for.

EDIT: Just noticed I was a bit late with my reply.

Comments

2

First off why not redirect with mod rewrite?

But anyways, you can concat $_SERVER['QUERY_STRING'] to the end of your url

2 Comments

I am finding mod_rewrite is really difficult to redirect query strings unless you know what they are going to be in advance. Either that or results in google search overcomplicate things.
@decker I think the google results you found are overcomplicating things. In principle it's no different than the accepted answer. Except with mod_rewrite instead of php.
1

Add the $_SERVER['REQUEST_URI'] or QUERY_STRING. Do a print_r($_SERVER); to see more information about the requested URL.

Comments

1

I would also suggest to use mod_rewrite for this task, you can be more flexible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.