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!')));
  1. Thanks for the functions! I didn’t want to modify my htaccess file, because it is based around security.

    I renamed them and placed in my generic functions class file (a non CakePHP system) and it works! These are going to help out a lot in my current projects.

  2. You’re wellcome. Yeah, that class is pretty simple and tottaly independent from CakePHP.
    I didn’t try yet the new versions of apache (since 2.2.12 when the bug was supposed to be fixed), but i believe that urlencode() will work from now and then. If you already tried it, please leave me your feedback. :-)

    • nikola
    • July 11th, 2010

    hi jtrancas,

    can you help me how and where i have to use your function in a case of encoding url, because i need to send some parameters using url?

    ie. when have to work with urls like: http://mydomain/name:one&two or http://mydomain/name:one?two

  3. @Nikola

    You can encode that parameters using this class, just UrlEncoder::encode() them, it seems you’re using named parameters from CakePHP, so you can do something like this:

    echo $this->Html->link(‘My link’, array(‘action’ => ‘do_something’, ‘my_parameter’ => UrlEncoder::encode(‘one&two’)));

    But remember you’ll have to decode it on the requested action.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: