homepage Welcome to WebmasterWorld Guest from 54.226.147.84
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

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



 
Msg#: 4414013 posted 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):

www.mywebsite.com/thispage.php?topic=43

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

www.mywebsite.com/what_is_the_capital_of_Peru

...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?

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4414013 posted 8:13 am on Feb 4, 2012 (gmt 0)

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.

AndyGroom



 
Msg#: 4414013 posted 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.

AndyGroom



 
Msg#: 4414013 posted 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" ?)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4414013 posted 7:18 pm on Feb 4, 2012 (gmt 0)

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
www.example.com/?id=43&text=what-is-the-capital-of-Peru or www.example.com/index.php?id=43&text=what-is-the-capital-of-Peru or for that matter
www.example.com/?text=what-is-the-capital-of-Peru&id=43 or www.example.com/index.php?text=what-is-the-capital-of-Peru&id=43 then you should redirect the user to www.example.com/43-what-is-the-capital-of-Peru 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved