Forum Moderators: phranque
Luckily there have been at least half a dozen similar discussions for this type of problem so far this month, which you can raid for example code.
You'll need to link to the new format URLs from within your pages as it is links that define URLs.
You'll need a rewrite to connect your URL request to the true internal filepath for the content or to a script that can look up the associations in the database.
Finally, you'll need a script that when asked for the old format URLs, sends out a 301 redirect to the new format URL so that the content can no longer be indexed as duplicates at the old URL.
This is not a simple job, because the association between /news/pages.php?nID=16&pID=822 (in numbers) and /news/second-news-item (in words, perhaps the title of the article?) cannot be gleaned within .htaccess nor programmed in advance.
You will have to go to your database to look up the association of URL and database entry in a new table that you have to add to your database - unless the second-news-item is actually the 'title' for the article, and is already in the existing database.
IF you simply wanted to change from /news/pages.php?nID=16&pID=812 to /news/16/812 then that would be very easy to achieve in .htaccess with less than a dozen lines of code, including canonicalisation fixes, because there is a very simple relationship between the old and the new URL.
The stuff about association is important. If you have a million pages, you do not want a URL structure such that you have to manually add one million redirects and one million rewrites into your .htaccess file.
You would need a simple system such that two lines of code, with a REGEX pattern match, can be used for all one million pages. It would map URL requests for example.com/news/23/543 to the internal filepath /index.php?cat=news&id=23&item=543 and so on. There would also be a redirect such that direct client requests for /index.php?cat=news&id=23&item=543 (and with those parameters in any other order) would be redirected to example.com/news/23/543 as well. Your internal links would also need to point to example.com/news/23/543 too.
With that idea in mind, you would be able to set up the new URL system in mere minutes, and not have to touch it again when adding new content.