Better URLs in PHP (without htaccess)

Posted Wednesday, June 8, 2005

Filed under: , , , ,

I wanted for a while to improve the readability of URLs on my website but without enough time to dive into htaccess. As a temporary fix I chose to expose the title in the URL, together with the id. Here's the how and why.

[EDIT: I have updated the function to replace accented characters and punctuation.]
First, you need to know that when it comes to SEO URLS and semantics matter. A lot.
Using various levels of header (instead of classes) boosted my google visibility very impressively, but semantics are mostly good for machines. So I moved along and after changing the page title according to the content (which is good for both humans and machines) I made my URLs more human-friendly, because a numeric id is not informative at all except to one database in particular.
I'm rewriting the whole backend so I didn't want to use htaccess and mod_rewrite for this, yet be able to reuse the components.
Even if you still can't have friendly URLs like archive/user-friendly-urls, something like index.php?id=123456/user+friendly+urls still is much more readable. And it only requires that you url-encode your titles to include them in the link. I use the following function:

function make_friendly_URL($input) {
$trans = array("" => "Y", "" => "u", "" => "A", "" => "A", "" => "A", "" => "A", "" => "A", "" => "A", "" => "A", "" => "C", "" => "E", "" => "E", "" => "E", "" => "E", "" => "I", "" => "I", "" => "I", "" => "I", "" => "D", "" => "N", "" => "O", "" => "O", "" => "O", "" => "O", "" => "O", "" => "O", "" => "U", "" => "U", "" => "U", "" => "U", "" => "Y", "" => "s", "" => "a", "" => "a", "" => "a", "" => "a", "" => "a", "" => "a", "" => "a", "" => "c", "" => "e", "" => "e", "" => "e", "" => "e", "" => "i", "" => "i", "" => "i", "" => "i", "" => "o", "" => "n", "" => "o", "" => "o", "" => "o", "" => "o", "" => "o", "" => "o", "" => "u", "" => "u", "" => "u", "" => "u", "" => "y", "" => "y", "I'll" => "I+will", "'" => "+", "(" => "", ")" => "", "!" => "", " " => "+", " - " => "+/+");
$input = strtr($input, $trans);
return $input;
}

Explanation: the array associates accents with their unaccented letter, and punctuation with an empty string. Also, since I often separate French and English in titles with space-dash-space I want this sequence to retain its sparative role.
Spaces are converted to + signs. At first I used an underscore (for readability) but then I discovered that it is a stop word (compare the number of results with key_word, key-word and finally key+word).
Now you just need to add this to your links, and let the world grow used to it...
As a last tip: google is reputedly the only search engine crawling pages after the ? sign, so beware and try to use the htacces method if you can.

[UPDATE: The following tutorial explains most of the uses .htaccess can be put to. Worth reading if you want to read further.]

Comments disabled because of spammers.

No comments yet

Technorati Profile