Flexible Content Management System


Url rewriting on Postnuke 0.8

Contributed by on Feb 25, 2008 - 11:07 PM

Main principles of the mod rewrite

For information, the URL rewriting is a module that you can activated in your apache to rewrite the links of a site in order to simplify their reading.

The idea is that the Pn Team also thought that the mod rewrite was not necessarily available / activated on all types of servers (particularly on Windows servers). Indeed, the Postnuke team offers rewritings based on tips already heavily used in management systems like blogs.

Before you begin, here is the format of a link without rewriting.

<legend>without rewriting</legend>
index.php?module=Users&func=logout Index.php? Users & module = func = logout

URL rewriting without mod rewrite

This rewriting reproduced a "virtual" Folders system on your site, links look like this

<legend>URL rewriting without mod rewrite</legend>

In terms of mechanism, it is very simple, when your Web server receives a request for a link, it loads instinctively page "index.php". In this index.php, the parameters of the request are recovered via the header (you can look in "phpInfo" there is a field $ _SERVER [ 'REQUEST_URI'] which corresponds to this information).Then these parameters are interpreted , between the first two "/" is the name of the module, between the two others, the function name. Now Postnuke know the name of the module and function to launch.

You can pass parameters too, for instance, if you want to load a forum with id=2, the links will look like this

<legend>URL rewriting without mod rewrite</legend>

Note that the url rewriting uses ":" to represent the parameters in a url. So you can't pass variables like this "index.php?variable=filter:3". (be carefull if you are using Pagesetter and his filters system).

URL rewriting with mod rewrite

Optimizing the previous version

In the previous example, all links contained index.php ... but it's ugly, and functionally this file contains no
information useful to load the asked module.This trick is useful when you have no mod rewrite, but if one has an "mod rewrite enabled" server,you can use a "lighter" version of the previous rewriting without an "index.php"

Here is an example

<legend>URL rewriting with mod rewrite :Optimizing the previous version</legend>

Be careful, if one of your pictures is written this way <img src="test.png"> and you load the page /MyModule/main/. "/MyModule/main/test.png" which will be searched. Note that you can make a rewriting that redirect all links of the form "/*/*/*.(jpg | png | gif)" to "$ 3. (Jpg | png | gif)." (but it's ugly).

Mode file

This url rewriting is the classical version already used in previous versions of the cms, The rewrited links are lists of words (module name, the name of function) separated by dashes. Note that this version uses a large number of regular expressions rules to do the rewriting compared to the other one, which may increase the load of your server Web.

One example here ...

<fieldset><legend>URL rewriting with mod rewrite : Mode file</legend>

Note that the "module" which one would have thought there's no point in it, is made for the support of the "old style" loading of modules.

URL rewriting customized

So here is the coolest feature, which allows you to customize the URL rewriting depending on the module you want to load. Just create a "encodeurl" function in your API module (pnuserapi) that takes as parameters, the information needed to create for output a fully customized rewrited link.

After, the loading of each page of the CMS, a "decodeurl" function in the API part of the module takes care to reformat the encoded url in a form understandable by the CMS.

You can find an example of the use of this method in the module "Pages" of values addons

<fieldset><legend>Format of a encode and decode url</legend>
function pages_userapi_encodeurl($args)
function pages_userapi_decodeurl($args)

This feature is interesting because it allows us to have the hands on your url, not only before loading the page, but after loading this page, allowing you post-processing actions rather interessant.

This method allow you to

  • - put keywords in the url: (see Pages module)

  • - have a single version of your rewrited url : Indeed, through the post treatment you can recalculate the url and it does not conform to your requirements, you can redirect to the good link via a redirect 301 "Moved Permanently headers.

Use case of a url rewriting "no duplicate"

Notes: A small problem nevertheless for the use of this rewriting, which is limited to the type url "user". (more details here)

This article is a translation from this "how to" released on Postnuke-France