how to redirect all dynamic pages to single php page using .htaccess

In core PHP sometimes we need to create dynamic pages for all the post added in the backed,if we have hundreds of post we don’t want to create 100 pages to display all the post.Here am going to display how we can use slug to identity each post and pass that slug in the URL suppose we have to create a URL like post_POSTNAME_page.so each post having the same URL structure we will replace POSTNAME with slug, that we created from the post tile or post name
so i want to call all post_POSTNAME_page to a single php page ,we can do it with the help of .htaccess .in the below post am going to explain you how to redirect all dynamic pages to single php page using .htaccess. First i need to create a php page name postpage.php
In the postpage.php we can get the value of slug by using the get method

$page_slug=$_GET[‘slug’];

by using this page slug we can use mysql to fetch all data related to that post

Here is the rules to redirect all dynamic pages to single php page using .htaccess


RewriteEngine On
Options +FollowSymLinks
RewriteRule post_(.*)_page$   postpage.php?slug=$1

See how it going to work

https://www.tutorialshore.com/post_Indian_history_page

It will call postpage.php?slug=indian_history

So if we use $_GET[‘slug’], we will get page slug Indian_history by using this slug we can access the post content for Indian history
same way you can add pages also

RewriteRule page_(.*)_content$ page.php?slug=$1

See All answers

  1. Hi,

    I’ve been visiting your website a few times and decided to give you some positive feedback because I find it very useful. Well done.

    I was wondering if you as someone with experience of creating a useful website could help me out with my new site by giving some feedback about what I could improve?

    Thank you for help and I wish you a great week!

Leave a Reply to SteveTinly Cancel reply

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator