What are some efficient and effective solutions for matching article titles with IDs in PHP databases for URL redirection?

When redirecting URLs based on article titles, it's important to have a mapping between the titles and their corresponding IDs in the database. One efficient solution is to create a separate table in the database that stores the article titles and their respective IDs. This table can be queried to retrieve the ID based on the title when redirecting the URL.

// Assuming you have a database connection established

// Query the database to retrieve the article ID based on the title
$title = $_GET['title'];
$query = "SELECT id FROM article_mapping WHERE title = :title";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':title', $title);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

// Redirect to the corresponding article based on the retrieved ID
if($row) {
    $article_id = $row['id'];
    header("Location: /article.php?id=$article_id");
    exit();
} else {
    // Handle case where article title is not found
    echo "Article not found";
}