我有传入的网址 http://www.example.com/index.html?id=1234
我有包含 2 列的数据库 WORDS:"id"和"url"
我需要 301 重定向将 url (1234) 中的 id 参数匹配到相应的 URL,例如:
http://www.example.com/index.html?id=1234 成为http://www.redirectedurl.com
我尝试了类似于此处解释的内容我认为正确的方法是拥有一个简单的 .htaccess 代码,该代码使用 php 文件将 id 与数据库中的 url 匹配,但我不知道从哪里开始。
感谢您的帮助!
<?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!";
}