Convert url strings to formatted urls


I have a site where I am querying a custom database based on a url string.

For example:

would query the database and return the candy type based on the candyid.

I’d like to “Pretty up” the url so that I can include the description of the candy along with the candyid and .html for SEO purposes:

For example:

Please note that the “candy” page is a physical page created in WordPress with a custom template to query the database and display the output.

Any direction is appreciated.