用于重定向页面的PHP数组

时间:2014-05-30 09:33:40

标签: php arrays

我目前有一个名为redirects.php的PHP文件,我用它来重定向特定请求,但是我有大约150个页面需要重定向到不同的新页面。到目前为止我的代码是:

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. "");
  }
}

复制if($ req ==“”){...正在变得非常长,并使文件变得相当大。我希望能够在一个数组行中执行此操作,例如:

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

在任何人提到使用.htaccess文件执行重定向之前 - 我通常会这样做,但是我们使用的是复杂的寻呼机文件,它忽略了301个请求。

4 个答案:

答案 0 :(得分:3)

就像你说的那样,使用数组。

您可以使用与$ _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");

答案 1 :(得分:0)

您可以创建一个类来检查数据库是否匹配,或者使用默认重定向发送到 - 或者可能不重定向:

<?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);

?>

如果你想将它保存在文件本身中,你可以快速执行array_search()以获得正确的值:

<?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.
    }

?>

答案 2 :(得分: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] . "");
}

从平面文件中读取数组可能更容易。

答案 3 :(得分:0)

试试这个:

$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!',
    }
}