Redirect Script

I need a script in PHP that’ll work like this:
redirect.php?url= would redirect to but I want it so I don’t have to manually add in every URL - I want it to redirect to the url in url=‘someurl’ so if my code was:


$url = $_REQUEST['url'];

//if ?url= then redirect to
if ($url=="")
header( 'Location:' );

If someone typed ?url= it wouldn’t work - I’d have to add it manually but I need something that’ll do this automatically (if redirect.php?url= then redirect to without me to have to add it manually). Please help if possible.

Use the GET method

$url = !empty($_GET['url']) ? trim($_GET['url']) : "" ;

Then later on before you re-direct… you can check $url to see that it’s not empty and re-direct if needed.

If you want automated redirect, I’d suggest using something like this:

header( 'Location: '.$_GET['url']);

If you want to exclude some domains from being redirected to, make it conditional:

$default = "";
$url = $default;
if (isset($_GET['url'])) { $url = $_GET['url']; }

if (strpos($url, "") { $url = $default; }
if (strpos($url, "") { $url = $default; }
if (strpos($url, "") { $url = $default; }

header( 'Location: '.$url);
Sponsor our Newsletter | Privacy Policy | Terms of Service