Felix Carver
Felix Carver

Reputation: 11

PHP Redirect using URL Params

I'd like to use PHP to create a redirect that captures the key from a url parameter, and rewrites it's associated value, for instance:

https://somewhere.com/folder/?a_param=123

to

https://somewhere_else.com/?b_param=123

Any guidance for a beginner would be appreciated here.

Upvotes: 0

Views: 466

Answers (2)

Clint
Clint

Reputation: 1073

use php header

header('Location: https://somewhere_else.com/?b_param=' . $_GET['a_param']);
exit();

Upvotes: 0

ryantxr
ryantxr

Reputation: 4217

Generally, in PHP you can redirect like this:

header("Location:" . $somewhere);
exit;

If you have a file called rd.php you would use code like this:

https://somewhere.com/folder/rd.php?a_param=123

In rd.php

<?php
$param = $_GET['a_param'];
header("Location: https://somewhere_else/?b_param={$param}");
exit;

If you want to implement https://somewhere.com/folder/?a_param=123, meaning you don't want the rd.php part, you will need some help from apache2 or whatever webserver you are using. You can use Apache mod_rewrite to do this.

    # Put this in document_root/folder
    # .htaccess
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ rd.php [QSA,L]

Upvotes: 1

Related Questions