views:

374

answers:

2

What is the most efficient way to realize php-driven permalinks?

Basically I want to reduce the database accesses to a minimum.

What is the best way to redirect to an id stored in the database?

A: 

Use a simple URL-to-ID map to retrieve the ID based on the URL:

+----------+----+
| URL path | ID |
+----------+----+
| /foo     | 1  |
| /bar     | 2  |
| /bar/baz | 3  |
| …        | …  |
+----------+----+
Gumbo
I think in my question is this approach included and not a really good answer that helps me. Nevertheless thanks.
ChrisBenyamin
@ChrisBenyamin: So what exactly do you want to know?
Gumbo
+6  A: 

You can use a db ID based url like SO does:

http://stackoverflow.com/questions/1265061/efficient-way-to-realize-permalinks-in-php

or

http://stackoverflow.com/questions/1265061

both go to the same place.

This is usually done through some sort of mod_rewrite redirect to your php file from a .htaccess.

RewriteRule ^/questions/([0-9]+)/?.*$ /questions.php?id=$1

The rewrite rule throws away everything after the ID - so you could even go to

http://stackoverflow.com/questions/1265061/not-the-questions-title-anymore

And you still reach your destination. You'll want to add the "title slugs" to the actual URL being 'linked' when you generate the links in php - it will improve your Search Engine Friendliness...

gnarf
You took the words of my keyboard ;) ! I may add that the first version is much more SEO-friendly !
Wookai
@gnarf: Yes that looks good. I will give your suggestion a try.
ChrisBenyamin
@wookai - added a mention about Search Engine Friendliness at your request ;)
gnarf
Except the "not-the-questions-title-anymore" leads to duplicate URLs. You should use either ID or "slug", not both. Unless you can guarantee uniqueness.
DisgruntledGoat
@DisgruntledGoat You can always use 301 redirection if you detect that the slug isn't correct if you are worried about those duplicate URL's
gnarf