logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: URL Encoding a variable in Apache
Author
csdude55



Joined: 22 Jan 2023
Posts: 23
Location: USA, Wilkesboro

PostPosted: Thu 11 Jul '24 20:12    Post subject: URL Encoding a variable in Apache Reply with quote

I'm trying to use %{REQUEST_URI} and %{QUERY_STRING} to redirect to a new url. For example, I start with this:

Code:
https://www.example.com/foo/?bar=1&lorem=2&ipsum=3


and I'm trying to redirect to this:

Code:
https://www.example.com/redirect.php?uri=/foo/&qs=bar%3D1%26lorem%3D2%26ipsum%3D3


I can be pretty flexible, but in redirect.php I just need to end up with a variable that looks like:

Code:
$uri = 'https://www.new.com/foo/?bar=1&lorem=2&ipsum=3'


I started out just using [QSA], but since I don't know the variables that'll be sent I ended up with a PHP script that is unnecessarily complex:

Code:
// starting with this:
// https://www.example.com/redirect.php?uri=/foo/&bar=1&lorem=2&ipsum=3

$uri = 'https://www.new.com' . $_GET['uri'];
unset($_GET['uri']);

if (!empty($_GET)) {
  $uri .= '?';

  foreach ($_GET as $key => $val)
    $uri .= $key . '=' . $val . '&';
}


Is there a way that I can URL encode %{QUERY_STRING}? If so, I could eliminate all of that and just use:

Code:
// I know this could leave a trailing ? if
// there's no query string, but that's OK
$uri = 'https://www.new.com' . ($_GET['uri'] ?? false) . '?' . urldecode($_GET['qs'] ?? false);
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Mon 15 Jul '24 21:59    Post subject: Reply with quote

use $_SERVER array.

'QUERY_STRING' and 'REQUEST_URI'.

Also take a look at the filter_input() function.
Back to top


Reply to topic   Topic: URL Encoding a variable in Apache View previous topic :: View next topic
Post new topic   Forum Index -> Apache