How to make nice URL for articles from database

In the book PHP&MySQL novice to ninja there is a recommendation to put all the PHP code to subdirectory on a server to achieve nice URL. But how to make nice URL for articles from the database when they have their own ID and have got dynamic URL with ID number? Would it be correct idea always at the same time to create a new folder when an ID article is created?

Would it be correct idea always at the same time to create a new folder when an ID article is created?

No it would not. It might be an idea to give the illusion you have created a new folder, but that can be done using apache’s mod_rewrite.

If you had “articles” and one of those articles was id number 23 and the title of that article was “Man bites dog”, then in order to achieve “nice urls” you could variously revert to any of:

/articles/man-bites-dog-23

/articles/man-bites-dog/23

/articles/23/man-bites-dog

/articles/23-man-bites-dog

and so on.

What you have to achieve is unique urls, one way to do that is to append or prepend the ID number (which will be unique) to the title which may not be unique, there may be another “man bites dog” story in a years time.

Others plump for this kind of uniqueness:

/articles/man-bites-dog-1234567890 (unixtimestamp

/articles/man-bites-dog/2012-06-05 (datestamp)

/articles/201206/man-bites-dog (year/month only - figuring that will be unique enough)

Given this particular case: /articles/23-man-bites-dog you would tell apache’s mod_rewrite to trap that incoming url, and just grab the 23 and redirect that to the url articles.php?id=23.

It might be better if you explained your idea of “nice urls”.

Depending on the size/scope of the site having /man-bites-dog could be beneficial for simplicity and possibly SEO. With this approach, you’d make “man-bites-dog” the primary key for the articles table to ensure each article has a unique URI.

Rewrite url using mode_rewite. Check it for beginner at http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/ to know how to make nice url for your articles