I have incoming url http://www.example.com/index.html?id=1234
I have database WORDS with 2 columns: 'id' and 'url'
I need to 301 redirect matching the id parameter in url (1234) to corresponding URL, such as:
http:// www.example.com/index.html?id=1234 becomes http:// www.redirectedurl.com
I tried something similar as what is explained here I think the correct way is having a simple .htaccess code that uses a php file to match ids with urls in database but i don't know where to start.
Thanks for your help!
<?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!";
}