Alex Rivera | Logout

URL Routing: Handling Spaces and Illegal Characters When Creating Friendly URLs

Asked 2008-11-05T21:16:33.953
9

I've seen a lot of discussion on URL Routing, and LOTS of great suggestions... but in the real world, one thing I haven't seen discussed are:

  1. Creating Friendly URLs with Spaces and illegal characters
  2. Querying the DB

Say you're building a Medical site, which has Articles with a Category and optional Subcategory. (1 to many). ( Could've used any example, but the medical field has lots of long words)


Example Categories/Sub/Article Structure:

  1. Your General Health (Category)
    • Natural Health (Subcategory)
      1. Your body's immune system and why it needs help. (Article)
      2. Are plants and herbs really the solution?
      3. Should I eat fortified foods?
    • Homeopathic Medicine
      1. What's homeopathic medicine?
    • Healthy Eating
      1. Should you drink 10 cups of coffee per day?
      2. Are Organic Vegetables worth it?
      3. Is Burger King® evil?
      4. Is "French café" or American coffee healthier?
  2. Diseases & Conditions (Category)
    • Auto-Immune Disorders (Subcategory)
      1. The #1 killer of people is some disease
      2. How to get help
    • Genetic Conditions
      1. Preventing Spina Bifida before pregnancy.
      2. Are you predisposed to live a long time?
  3. Dr. FooBar's personal suggestions (Category)
    1. My thoughts on Herbal medicine & natural remedies (Article - no subcategory)
    2. Why should you care about your health?
    3. It IS possible to eat right and have a good diet.
    4. Has bloodless surgery come of age?

In a structur

Edit
Report

3 Answers

1

Solution 2 is the typical approach of those... some refinements are possible, eg. turning apostrophes into nothing instead of a dash, for readability. Typically you will want to store the munged-for-URL-validity version of the title in the database as well as the ‘real’ title, so you can select the item using an indexed SELECT WHERE.

However. There is no actual illegal character in a URL path part, as long as you encode it appropriately. For example a space, hash or slash can be encoded as %20, %23 or %2F. This way it is possible to encode any string into a URL part, so you can SELECT it back out of the database by actual, unchanged title.

There are a few potential problems with this depending on your web framework though. For example anything based on CGI will be unable to tell the difference between an encoded %2F and a real /, and some frameworks/deployments can have difficulty with Unicode characters.

Alternatively, a simple and safe solution is to include the primary key in the URL, using the titled parts purely for making the address nicer. eg.:

http://www.example.com/x/category-name/subcat-name/article-name/348254863

This is how eg. Amazon does it. It does have the advantage that you can change the title in the database and have the URL with the old title redirect automatically to the new one.

answered 2008-11-05T21:45:20.150
1

In case anyone is interested. This is the route (oooh... punny) I'm taking:

Route r = new Route("{country}/{lang}/Article/{id}/{title}/", new NFRouteHandler("OneArticle"));
Route r2 = new Route("{country}/{lang}/Section/{id}-{subid}/{title}/", new NFRouteHandler("ArticlesInSubcategory"));
Route r3 = new Route("{country}/{lang}/Section/{id}/{title}/", new NFRouteHandler("ArticlesByCategory"));

This offers me the ability to do urls like so:

  • site.com/ca/en/Article/123/my-life-and-health
  • site.com/ca/en/Section/12-3/Health-Issues
  • site.com/ca/en/Section/12/
answered 2008-11-07T09:00:22.517
0

What I do normally is to allow only legal character and keep the friendly URL as short as possible. Also important is that friendly URLs are often inserted by human, I never generate a friendly URL from title or content, and then use that one to query the database. I would use a column in a table e.g. friendly_url, so that the website admin can insert friendly URLs.

answered 2008-11-05T21:27:10.233

Your Answer