如何重定向页面分析url键入?

时间:2016-07-22 16:43:23

标签: javascript php jquery html ajax

我需要一个页面,当我加载它时,请查看键入的链接,例如:
www.test.com/myphp.php=10
如果它找到某个数字,它会重定向到另一个链接,例如:
发现www.test.com/myphp.php=10重定向到新闻www.test.com/news/10.php

我该怎么做?我应该使用什么技术?

4 个答案:

答案 0 :(得分:1)

您想要使用$_GET变量,如此

http://www.test.com/myphp.php?p=10

对于你的php,你可以做到这一点。

header('Location: /news/' . $_GET['p'] . '.php');
die();

答案 1 :(得分:1)

首先,你的GET变量的语法有点偏。我假设您的意思是test.com/myphp.php?redirect=10或类似名称的起始网址?因为www.test.com/myphp.php=10语法不好。

我假设我们正在使用GET变量,如上所述:

最简单的方法是在PHP中设置位置标题:

if(array_key_exists("redirect", $_GET)){
    #set header to redirect to new location
    header("Location: /news/" . $_GET["redirect"] . ".php");
    #don't let the page do anything else
    die();
}else{
    #do something if the GET variable doesn't exist
}

请注意,这种方式会引入一些安全漏洞,因此您可能希望执行更高级的操作(例如intval GET变量,以便它们无法将脚本注入您的变量,或仅addslashes() 1}}到GET变量值)。

答案 2 :(得分:1)

Url应该带有get参数名称。 http://www.test.com/myphp.php?news=10

if(isset($_GET['news']) && is_numeric($_GET['news'])) {
     $newsUrl = 'http://www.test.com/news/' . (int)$_GET['news'] . '.php';
     header('location: ' . $newsUrl);
     // or 
     // header('location: /news/' . (int)$_GET['news']);
     die;
}

答案 3 :(得分:-2)

普通的旧javascript

function getQueryVariable(variable)
{
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
   }
   return(false);
}

如果网址是www.test.com/myphp.php?id=10 调用getQueryVariable(&#34; id&#34;)将返回10

然后您只需使用

重新路由
var number = getQueryVariable('id')
window.location = "www.test.com/news/"+number+".php";
相关问题