'How to set same path for category and post using htaccess in PHP?
I am creating a blog system in PHP. I created categories and posts in my database. I have two pages to display categories-related posts and post viewer. I want to open category and post in the same path using .htaccess . I have tried this htaccess code .
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ show.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ catlinks.php?id=$1 [L]
As you can see above, I created show.php for the post viewer and catlinks.php for the category-related posts.
when I open URL like -
example.com/postSlug
it works but when I open a category in the same path
example.com/categorySlug
it opens show.php and not found page.It doesn't point catlinks.php page
I can do it like this example.com/category/categorySlug. but I want to open on the same path -
like
example.com/this-is-my-post
example.com/category-slug
Is it possible to open on same path?
Solution 1:[1]
Rewrite rules to point only one file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ show.php?id=$1 [L]
Now, we are pointing to one file. In the same file, we can create two conditions when a post is found else when the category is found.
It all depends on your slug. The slug should be unique for post and category.
The solution is taken from https://technosmarter.com/php/create-categories-in-blog-using-php-and-mysql
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vishal Rana |
