Ad

How To Implement Slug Replacing Id's In The URL

- 1 answer

Presently I have a home page (index.php) which contains several links to an another page (story.php).In order to load different stories my hyper links are as follows

<a  href=”story.php?cat_id=0&n_id=2”>Best Mobile Phones of 2016</a>

As you can see that the URL is like : story.php?cat_id=0&n_id=2

I want to change the URL into a Slug like Best-Mobile-Phones-of-2016.The slugs are present as a field in a table.

Now my question is if I want to serve pages using slugs and not by id’s, then how should I go about?I mean to say that how should be my hyperlinks to different stories in the index.php.

I hope that you understand my question. Any help in this regard would be highly appreciable.

Ad

Answer

SEO friendly links basically look like this: story/Best-Mobile-Phones-of-2016 instead of story.php?cat_id=0&n_id=2.
Now when a user surfs that site, your .htaccess rewrites that url to e.g. story.php?storyID=Best-Mobile-Phones-of-2016. Your algorithm has to figure out which cat_id and n_id is associated with the storyID.

You can also append the cat_id and n_id to your storyID: story.php?storyID=Best-Mobile-Phones-of-2016_0_2. Then your algorithm can easily explode your storyID.

Hope that helped you.

Ad
source: stackoverflow.com
Ad