How do I solve this issue?

I recently got this from a friend and want to solve this issue. any advice please.

A website contents are completely based on mysql database + php.

Its links are currently shown as:
mysite.com/cat_id=147
mysite.com/cat_id=148
mysite.com/cat_id=149
mysite.com/topic_id=2302
mysite.com/topic_id=2303
mysite.com/topic_id=2304

I want to convert that links to search engine optimized as follows:
mysite.com/category/news/
mysite.com/category/books/
mysite.com/category/music/
mysite.com/topics/topic_title_1
mysite.com/topics/topic_title_2
mysite.com/topics/topic_title_3

How can I do it? I’m ready to change whole website coding.

But, I don’t know how.

where do I start?

Do I need to make any changes on that website’s database?

Could you please give me a php code sample (to solve one link for example)?

Please help. or give links to solve this.

Thanks in advance

The term you want to research is search engine friendly URLs, most often accomplished with Apache’s ModRewrite.

You will need to change your DB to also store the URL segment (e.g topic_title_2) so can find the page or article based on that, instead of the numeric ID.

It looks like all your current URLs use index.php?

An example .htaccess file (Apache configuration) might look like this:


RewriteEngine On
RewriteRule category/([a-zA-Z0-9\\.\\_\\-]+)/?$ index.php?category=$1
RewriteRule topics/([a-zA-Z0-9\\.\\_\\-]+)/?$ index.php?topic=$1

With this a request for category/books will point to index.php?category=books (the address bar won’t change, but index will have $_GET[‘category’]).
You’ll then need to lookup your DB for the correct content.

If you have a CMS for adding items to the DB it will need to create a unique URL segment for each record.

This might seem radical but you could just change your database.

From:

Categories

cat_id 149
cat_title ‘Local News’

To

Categories

cat_id ‘local-news’
cat_title ‘Local News’

Or just plain

Categories

cat_title ‘Local News’

(Then be prepared to lowercase and ‘slugify’ the title e.g. “Local news” might be your nice Title on page, whereas ‘local-news’ might be your slug)

Now of course you have to change your dependent tables too. :wink:

Here is an old long and rambling post of mine if you are interested in why/how this can be done and the benefits, and also introduces some terms which might be new to you.

Basically, you want to do away with having a script which has to decipher 149 to discover its real meaning “Local News”, but you should continue on with it if you feel the database solution is over the top.

Personally I’m not that keen on automatically converting local-news to Local News, because it’s hard to know when a dash is a substitute for a space, or when it really was supposed to be part of the title.
With a lookup there’s no confusion. The primary key could be a string field though.

Thank you very much for everyone!