0

Right now my URL looks like: https://www.example.com/blogitem.php?id=29

What I want is the user will see the URL in their browser like: https://www.example.com/blogitem/the-title-of-the-current-blog

This thing is already implemented in Wordpress and other CMS but I am using pure PHP and don't have any idea about how can I implement this feature. Can anyone please help me to implement this?

Soumyajit
  • 329
  • 1
  • 5
  • 16
  • 4
    Possible duplicate of [How to create friendly URL in php?](https://stackoverflow.com/questions/812571/how-to-create-friendly-url-in-php) – Spoody Dec 16 '17 at 10:35

1 Answers1

1

write this on code .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?id=$1 [L,QSA]

you need to get title form database instead of id so it will be like this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ blogitem.php?title=$1 [L,QSA]
Kiwagi
  • 168
  • 5
  • 17
  • I added this code to .htaccess file but the URL patterns remains same... – Soumyajit Dec 16 '17 at 11:38
  • 1
    if you have added this `RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ blogitem.php?id=$1 [L,QSA]` and try url on browser https://www.example.com/29 – Kiwagi Dec 16 '17 at 12:36
  • This does the trick... if I want the URL like "https://www.example.com/blog/the-title-of-the-blog" then how can I configure the .htaccess file? – Soumyajit Dec 16 '17 at 14:52
  • you have to select the-title-of-the-current-blog from the database instead of Id on your url . then you change this line `RewriteRule ^(.*)$ blogitem.php?id=$1 [L,QSA]` to this `RewriteRule ^(.*)$ blogitem.php?the-title-of-the-current-blog=$1 [L,QSA]` – Kiwagi Dec 16 '17 at 15:05
  • I fetched all the required titles from DB... currently it shows the URL like this: "https://www.example.com/title-of-the-blog" but I want like this: "https://www.example.com/blog/title-of-the-blog". Notice the "/blog/" in the middle of the URL – Soumyajit Dec 16 '17 at 15:30
  • `blogitem.php?the-title-of-the-current-blog=/blog/$1 [L,QSA]` OR create a folder name it blog and insert blogitem.php file in that folder. – Kiwagi Dec 16 '17 at 18:17