2

I currently have a PHP file called redirects.php which I am using to redirect particular requests, however I have around 150 pages I need to redirect to different, new pages. My code so far is this:

if (isset($_GET['req'])) {
  $req = $_GET['req'];
  $redirect = "";

  if ($req == "quoter_buying_guides.php") {
    $redirect = "newpage";
    $loc = "http://www.example.co.uk/" . $redirect;
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: " .$loc. "");
  }
}

Copying the if ($req == "") {... is getting very long winded and making the file rather large. I'd like to be able to do this in one array line for example:

"old-page.php" => "new-page.php
"old-page2.php" => "new-page2.php";

Before anyone mentions using the .htaccess file to perform redirects - I would normally have done however we're using a complicated pager file which ignores 301 requests.

8
  • Have you considered keeping the redirects in a database and simply querying the one you need to get back a single record of where to send the user? Commented May 30, 2014 at 9:34
  • You have the correct idea. So what is the problem? Array access is a pretty trivial issue. Commented May 30, 2014 at 9:37
  • @Fluffeh: How is querying a database different than querying an array in this context, apart from the fact that it introduces extra complexity? We 're talking a couple hundred pages, which is a trivial amount of data. Commented May 30, 2014 at 9:37
  • 1
    @Jon The OP mentions that the file is getting very long already, as it gets longer, it is only going to make redirects harder and harder to keep track of. If you pop them into a db, you can not only keep them in order, but also very easily track redirect counts or dates. Commented May 30, 2014 at 9:50
  • 1
    @Fluffeh: And I just wanted to make sure we 're not over-engineering here. No worries, have a nice day! Commented May 30, 2014 at 10:15

4 Answers 4

4

Just like you say, use an array.

You can use the same key as what you provide in the $_GET['req']

//Define all redirects here
$redirects = array();
$redirects['quoter_buying_guides.php'] = 'new-page.php';
$redirects['key2'] = 'new-page2.php';

//Or like this
$redirects = array(
   'quoter_buying_guides.php' => 'new-page.php',
   'key2' => 'new-page2.php'
);

//Here we do the actual redirect
if(isset($_GET['req']) && isset($redirects[$_GET['req']])) {
    $loc = "http://www.example.co.uk/" . $redirects[$_GET['req']];
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: " . $loc);
    exit();
}
//Nice to have
header("HTTP/1.1 404 Not Found");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Sander! This soluton worked perfectly and saved me a hell of a lot of time. I had an exported CSV of over 150 URL's which I was literally able to paste into my redirects.php file and build the array around it and redirect each individual page to the relevant, corresponding page. Thanks again.
Hey, just want to say, thanks for this! I've worked on this to be able to be used for wildcard redirects too which is absolutely perfect. Here's my answer on my thread with the full gem of a script it is :)
0
if (isset($_GET['req'])) {
  $req = $_GET['req'];

  $redirs = array ('quoter_buying_guides.php' => 'http://www.example.co.uk/newpage',
                   'foo' => 'http://www.example.co.uk/bar');
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: " .$redirs[$req] . "");
}

It'd probably be easier to read the array in from a flat file.

1 Comment

This assumes that the requested url is always set in the $redirs array.. i would not recommend using this @Sander Visser is a better solution
0

You could make a class that checks the DB for a match or otherwise uses a default redirection to send to - or perhaps does not redirect:

<?php

    class redirecto
    {
        public $outLoc;
        public function __construct($input)
        {
            // Here you run a query to get the matching 
            // redirection from a database
            // or return false/empty when not found.
            $redirect='http://www.example.com';

            if(redirect)
            {
                $outLoc = "http://www.example.co.uk/" . $redirect;
            }
            else
            {
                $outLoc = "http://www.example.co.uk/someDefault";
            }
            header("HTTP/1.1 301 Moved Permanently");
            header("Location: " .$outLoc. "");
        }
    }

    $checkRedirect=new redirecto($yourRequest);

?>

If you do want to keep it inside the file itself, you can do a quick array_search() to get the right value back:

<?php
    $array = array(
        "old-page.php" => "new-page.php"
        "old-page2.php" => "new-page2.php",
        );

    $redirect = array_search('old-page.php', $array);

    if($redirect)
    {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: " .$redirect. "");
    }
    else
    {
        // either no redirect or default location.
    }

?>

Comments

0

try this:

$new_URL = array(
                'old-page.php' => 'new-page.php',
                'old-page2.php' => 'new-page2.php',
                'old-page3.php' => 'new-page3.php',
                'old-page4.php' => 'new-page4.php',
                'old-page5.php' => 'new-page5.php',
                'old-page6.php' => 'new-page6.php',
                'old-page7.php' => 'new-page7.php',
                'old-page8.php' => 'new-page8.php',
                'old-page9.php' => 'new-page9.php',

        );

if (isset($_GET['req']))
{
    if(array_key_exists($_GET['req'], $new_URL))
    {
        $loc = 'http://www.example.co.uk/'.$new_URL[$_GET['req']];
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: '.$loc.'');
    }else{
        echo 'requested URL not found!',
    }
}

1 Comment

This assumes that the requested url is always set in the $new_URL array.. i would not recommend using this @Sander Visser is a better solution

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.