Asked  7 Months ago    Answers:  5   Viewed   39 times

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

 Answers

79
<?php
    header("Status: 301 Moved Permanently");
    header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
    exit;
?>
Wednesday, March 31, 2021
 
mgraph
answered 7 Months ago
79

This will give you the total number of & separated URL query parameters:

count(explode('&', $_SERVER['QUERY_STRING']))

If you only want unique parameters, use $_GET instead:

count($_GET)
Wednesday, March 31, 2021
 
nomie
answered 7 Months ago
87

The redirect is sent to and processed by the client which responds by navigating to the given Location: index.php. You should not expect that the custom header is provided by the browser when it is requesting index.php from the server.

CLIENT                                    SERVER
 |------- POST login.php ------------------>|
 |                                          |
 |<- 200, Location: index.php, X-Test:test -| (this is where you send the header)
 |                                          |
 |------- GET index.php ------------------->| (no header from client to server)
Wednesday, March 31, 2021
 
ClmentM
answered 7 Months ago
90

You CAN header redirect a POST request, and include the POST information. However, you need to explicitly return HTTP status code 307. Browsers treat 302 as a redirect with for GET, ignoring the original method. This is noted explicitly in the HTTP documentation:

  • https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8

Practically, this means in PHP you need to set the status code before the redirect location:

    header('HTTP/1.1 307 Temporary Redirect');
    header('Location: anotherpage.php');

However, note that according to the HTTP specification, the user agent MUST ask user if they are ok resubmitting the POST information to the new URL. In practical terms, Chrome doesn't ask, and neither does Safari, but Firefox will present the user with a popup box confirming the redirection. Depending on your operating constraints, maybe this is ok, although in a general usage case it certainly has the potential to cause confusion for end users.

Wednesday, June 2, 2021
 
adjco
answered 5 Months ago
90

This should work, I tested it with some different names and dirs, but that should be ok in your case.

NB: for matched group from the RewriteCond you must use %1 not $1.

RewriteCond %{QUERY_STRING} ^s=([a-z]+)$ [NC]        
RewriteRule ^$ /s/%1? [NC,R,L]                     

RewriteRule ^s/([a-z]+)$ /?s=$1 [NC,L] 

Edit for debug (see comments) :

my test is :

| /
| --> doc
|   |
|   --> doc.php (takes doc as GET parameter)
|     | index.php

My apache rewrite is

RewriteCond %{QUERY_STRING} ^doc=([a-z]+)$ [NC]
RewriteRule ^$ /doc/%1? [NC,R,L]

RewriteRule ^doc/([a-z]+)$ /doc/doc.php?doc=$1 [NC,L]

Then asking for domain.com/?doc=query displays doc is query

Works for me.

Tuesday, August 31, 2021
 
toolic
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :