Posts Tagged ‘ urlencode ’

Url encoding, mod_rewrite, and pretty url’s

While using CakePHP sometimes you face a situation where you need to pass an url (or any other piece of text) as argument to another action, and your first impulse to do that is to look at the PHP function list and pick the urlencode() one, so you go ahead and urlencode() your data happily. But sadly, when you click on that link, you get an HTTP 404 not found error, why that? That happens due to a bug at Apache’s mod_rewrite module, it decodes permaturely the encoded ‘/’ present in url’s, it seems to be fixed after 2.2.12 but i leave here a workaround anyway.

Sometimes people ask for a solution in community and i see answers like “you can always pass a base64 representation for your data”, but there’s a little problem with that, the Base64 encoding uses the following 64 characters [A-Za-z0-9], “/” and  “+”, as you can see the last two chars are not friendly for this use, what solution do we have?

We can base64 encode it, and replace all nocive characters by a conventional ones, when decoding we do exactly the opposite and everything will work like a charm.

One cake way for doing that:

Well.. we could need it everywhere, so the best way to implement it is as a Library, with CakePHP 1.3 we have a “libs” directory under our app, so we can create there a file called “url_encoder.php” with the following code:


class UrlEncoder {

    public static function encode($url) {
        return preg_replace(array('/\+/', '/\//'), array('-', '_'), base64_encode($url));

    public static function decode($url_encoded) {
        return base64_decode(preg_replace(array('/-/', '/_/'), array('+', '/'), $url_encoded));


And now you could import it and use it anywhere!:

App::import('Lib', 'UrlEncoder');
echo $this->Html->link('My link', array('action' => 'do_something', 'some_data' => UrlEncoder::encode('My beautifull/nocive data!')));