0

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.

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

This was super helpful and got the job done. Unfortunately I've just discovered that the website where I'm trying to get this script to function is actually running on Squarespace and they don't allow custom server-side scripts. So... is it possible to achieve the same with Javascript? Appreciate this deviates quite a bit from my initial question...
I think you can. Open a new issue.
0

use php header

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

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.