Redirect Script

I need a script in PHP that’ll work like this:
redirect.php?url=http://example.com would redirect to example.com 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:

<?php

$url = $_REQUEST['url'];

//if ?url=http://google.com then redirect to http://google.com/
if ($url=="http://google.com/")
{
header( 'Location: http://www.google.com/' );
}
?>

If someone typed ?url=http://microsoft.com it wouldn’t work - I’d have to add it manually but I need something that’ll do this automatically (if redirect.php?url=http://example.com then redirect to http://example.com 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 = "http://www.defaultwebsite.com";
$url = $default;
if (isset($_GET['url'])) { $url = $_GET['url']; }

if (strpos($url, "http://www.microsoft.com") { $url = $default; }
if (strpos($url, "http://www.yahoo.com") { $url = $default; }
if (strpos($url, "http://www.live.com") { $url = $default; }

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