创建一个处理多个目的地的重定向页面?

时间:2013-12-25 16:43:44

标签: php redirect

我有一个大约50页的网站,每个网站都会将访问者发送到不同的外部网址,目前直接链接到外部网域。

我正在重建网站,并希望访问者首先通过内部重定向页面。

如何创建一个可以处理所有这50个不同目的地的重定向页面,而不是构建50个不同的重定向页面?

我想我可以将链接转换为

http://mydomain.com/redirect.php?destination=[1/2/3/4/.../50]

但是如何在redirect.php上定义(以最简单的方式),
每个目标号码的匹配外部网址?

最好使用一些简单的.txt文件,比如

1 http://external-1.com/
2 http://external-2.com/
.
.
50 http://external-50.com/

请注意,外部URL也由一些PHP变量组成, 例如,

http://external-1.com/?id=<?php print $variable; ?>

由于

3 个答案:

答案 0 :(得分:0)

function get_url($lineNo){
   $array = file("domain.txt");
   return preg_replace('/[0-50]+\. /', '', $array[$lineNo]); //removes numbering from result
}

您需要做的就是

http://mydomain.com/redirect.php?destination=<?php echo get_url(46) ?>

这假定域名没有。与行号位于同一行。

编辑:为您做了一些工作。现在你可以写行号了。还有!

编辑2

问)如果我的.txt文件看起来像这样:'1 external-1.com/?id='和我的redirect.php有这个命令:''

A)将功能更改为:

function get_url($lineNo){
   $array = file("domain.txt");
   $domain = preg_replace('/[0-50]+\. /', '', $array[$lineNo]); //removes numbering from result
   return $domain.'?id='.$lineNo; //appends $_GET['id'] to the found domain.
}

因此,如果搜索到的域名为1. http://www.domain-1.com/,则会返回http://www.domain-1.com/?id=$lineNo

答案 1 :(得分:0)

您可以构建一个包含所有目标网址的数组,例如:

$target = array(0 => "target1.com/something", 1 => "target2.com/somethingelse");

只需重定向

header('location:' .$target[$_GET['destination'])

我正在使用您的网址http://mydomain.com/redirect.php?destination=[1/2/3/4/.../50]

的示例

答案 2 :(得分:0)

您需要提供一个参数来标识要重定向的页面。对于外部页面,请检查您的主机名称。您需要为数组或txt文件中的每个外部站点保留映射。例如“1”=&gt; “externalsite.com1”。如果你想在文本文件中映射它,那么你必须阅读它,并需要通过其密钥检索网址。您的Redirect php将如下所示:

        if(isset($_REQUEST['destination']){

        switch ($_REQUEST['destination']) {
            case "1":
                //retrive the url corresponding the key  "1" and $redUrl= retrived url ;
                break;
            case "2":
                //retrive the url corresponding the key  "2" and $redUrl= retrived url  ;
                break;
            case "3":
                //retrive the url corresponding the key  "3" and $redUrl= retrived url  ;
                break;
        }
header("location:".$redUrl);
}