Hi, I have a URL that looks like this blog.php?id=1
How can I rewrite it to the blog post title? example.com/blog-title-of-the-post
And then how to locate the correct post when someone inputs rewrite URL?
I mean from ID to title and then from title to ID?
I found this solution:
$id = (int) $_GET['id'];
// suppose connection to database is established already
$result = mysql_query("SELECT `title` FROM `articles` WHERE `id` = $id");
if (!$result) {
// there is no such an article
// you can redirect to some other page, or show some error message
} else {
$row = mysql_fetch_assoc($result);
$title = $row['title'];
$urlPart = generateURL($title);
header('location:/articles/$id/$urlPart');
exit;
}
But what should I do when someone inputs already rewrite URL? Since there is no ID how to fetch the ID to be used in the SQL query?
I guess I need htaccess rewrite code?