views:

37

answers:

2

Hi,

I have a news section where the pages resolve to urls like

newsArticle.php?id=210

What I would like to do is use the title from the database to create seo friendly titles like

newsArticle/joe-goes-to-town

Any ideas how I can achieve this?

Thanks,

R.

+2  A: 

I suggest you actually include the ID in the URL, before the title part, and ignore the title itself when routing. So your URL might become

/news/210/joe-goes-to-town

That's exactly what Stack Overflow does, and it works well. It means that the title can change without links breaking.

Obviously the exact details will depend on what platform you're using - you haven't specified - but the basic steps will be:

  • When generating a link, take the article title and convert it into something URL-friendly; you probably want to remove all punctuation, and you should consider accented characters etc. Bear in mind that the title won't need to be unique, because you've got the ID as well
  • When handling a request to anything starting with /news, take the next part of the path, parse it as an integer and load the appropriate article.
Jon Skeet
An additional benefit is that it makes retrieval easier and faster - you just have to use a numeric ID instead of a potentially long string.
Archimedix
that's all good info. The platform in linux running php. Any links to where to get started would be a great help.
Roscoeh
@Roscoeh: I'm afraid I don't know enough about PHP to give detailed advice on that front. Producing the URL should be reasonably straightforward, but I don't know how routing works in PHP.
Jon Skeet
A: 

Assuming you are using PHP and can alter your source code (this is quite mandatory to get the article's title), I'd do the following:

First, you'll need to have a function (or maybe a method in an object-oriented architecture) to generate the URLs for you in your code. You'd supply the function with the article object or the article ID and it returns the friendly URL with the ID and the friendly title.
Basically function url(Article $article) => URL.

You will also need some URL rewriting rules to remove the PHP script from the URL. For Apache, refer to the mod_rewrite documentation for details (RewriteEngine, RewriteRule, RewriteCond).

Archimedix