Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

How do you map a database entry to a readable folder name?



7:35 pm on Feb 3, 2012 (gmt 0)

I apologise if this question has been answered but I don't know what to search for in order to find the solution!

I am creating a website in which all the pages are generated by PHP from a central database. To display a page, the URL would be something like this (like a phpBB forum):


If topic 43 happens to be "what is the capital of Peru", I'd much rather be able to link to the page thus:


...which would have the same effect as going to thispage.php?topic=43. How do I achieve this without creating a folder for every topic in the database?


8:13 am on Feb 4, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

If you would be OK with URLs like www.example.com/43-what-is-the-capital-of-Peru then this can be done using a few simple rewrite rules and a bit of PHP programming.

You would also have to alter the links on the pages to point to the correct URLs. Are there any "URL rewriting" packages already available for the forum software you use?

One note. Never use spaces or underscores in URLs.


9:11 am on Feb 4, 2012 (gmt 0)

It isn't actually a forum I'm running, it's something I've started from scratch (I realise now that mentioning phpBB was misleading!). So to answer your question, no there are no URL rewriting packages.

Can you post or point me to an example of how to do the 43-what-is-the-capital kind of thing? Although I'm quite new to PHP I am getting the gist of it so I could probably implement it.


12:10 pm on Feb 4, 2012 (gmt 0)

(I gather than mod_rewrite is the key to achieving what I want to do, but how do I create a rule which just takes the "43" from "43-what-is-the... etc" to redirect to "thispage.php?topic=43" ?)


7:18 pm on Feb 4, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

When you want to link to the URL that will show page "43" of your site, look in the title table of your database for the page title of page 43 and then construct the href for the URL by using the page number and that page title (normalised to lower case, with spaces and other punctuation replaced with hyphens).

When the user clicks on href="/43-what-is-the-capital-of-Peru" the browser sends a request to the server. This RewriteRule will call your script...

RewriteRule ^([0-9]+)-(.*) /index.php?id=$1&text=$2 [L]

Your script first needs to check (by looking in the database table) that the ID is valid. If not, send a 404 HEADER and a human readable page of html and text with an error message. Next, check that the requested page title is correct for this page ID. If the title is not valid for this ID, send a 301 redirect to the correct URL. If the ID and title are both valid, pull the page content from the content table of the database, then build the HTML page and send it to the browser.

At no point do you "redirect" to a parameter-based URL. Instead you should internally "rewrite" requests to be internally served by a parameter based internal filepath.

If someone does request
or for that matter
then you should redirect the user to
to prevent duplicate content issues.

This RewriteRule is the reverse of the first example, but needs an additional bit of code to prevent an infinite redirect-rewrite loop. This extra line of code is a RewriteCond that checks THE_REQUEST to ensure that only parameters in an external request are redirected, not those from a previous internal rewrite.

Featured Threads

Hot Threads This Week

Hot Threads This Month