'Rewrite URL using .htaccess display 404
I have a blog folder and in the blog folder, I have 2 files. index.php and post.php. Index for display all articles I write and post.php to display details about all articles. My .htaccess file has this code:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]
RewriteEngine On
RewriteRule ^/blog/(.*)$ ^/blog/post.php?link=$1 [L]
In my post.php I have this code:
if(!empty($_GET['link'])) {
$id = $_REQUEST['link'];
}
if (null === $id) {
header('index');
} else {
$sql = "SELECT * FROM blog WHERE link = :link";
$query = $dbh->prepare($sql);
$query->bindParam(':link', $id, PDO::PARAM_INT);
$query->execute();
$project = $query->fetch(PDO::FETCH_ASSOC);
}
In the database column link have a value of SEO URL. The problem is when I open example.com/blog/ I get all articles I write, but when I open a random article, I got a 404 error, what is wrong with my code?
Note: if I change my .htaccess to this:
RewriteEngine On
RewriteRule ^blog/(.*)$ blog/post.php?link=$1 [L]
Now when I open example.com/blog/I got a details page not a list of all my articles from the database and the SEO URL works. So example.com/blog/ and example.com/blog/random-article are the same and that shouldn't be like that. Thanks for the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
