我有传入的网址http://www.example.com/index.html?id=1234
我有2列数据库词:'id'和'url'
我需要301将url(1234)中id参数的匹配重定向到相应的URL,例如:
http:// www.example.com/index.html?id=1234成为 http:// www.redirectedurl.com
我尝试了类似于解释here的内容 我认为正确的方法是使用一个简单的.htaccess代码,该代码使用php文件将id与数据库中的url相匹配,但我不知道从哪里开始。
感谢您的帮助!
答案 0 :(得分:0)
<?php
//Check if GET value is set
if(isset($_GET["id"]){
//Retrieve values from database
$db = new PDO(/* connect to your database here */);
$stmt = $db->prepare("SELECT `url` FROM `table` WHERE `id`=? LIMIT 1");
//If the statement doesn't execute
if(!$stmt->execute(array(trim($_GET["id"])))) exit("Could not connect to database!");
//If there are no rows returned
if($stmt->rowCount()<1) exit("Could not find ID!");
//Data is correct - redirect the user!
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$url = $row["url"];
header("Location: ".$url);
exit;
} else {
echo "No ID set!";
}