Jump to content

Ocs-server/Gfx4/URL Rewriting

From KDE Community Wiki

Gfx contains a simple but working url rewriter that works in conjunction with the MVC system. It basically translates an URL asked by the client into a new one elaborated by the server.

All the rules are in the file config/rewrite.conf.php. This is an example rewrite config file:

    <?php die(); ?>
    /help|/help/index
    /yourpath|/install/index

Notice how the first URL is the one written by the client and the second one is that one elaborated by the server, when handling the request of a webpage.

Everything that comes after a controller is usually handled as a parameter, except when you rewrite the URL. So, for example, calling /help/index/arg1/arg2/arg3 will be directly passed as an argument array to the method index($args) of the controller HelpController. Every other standard GET parameter can be passed as usual:

   http://www.example.com/help/index?x=5
   http://www.example.com/help/index/?x=5
   http://www.example.com/help/index/arg1/arg2?x=5
   http://www.example.com/help/index/arg1/arg2/?x=5

A little example:

<?php die(); ?>
/|/main/index|exact
/help|/help/index|normal
/games|/games/lates|normal

And then it will rewrite doing a simple string replace, but internally on the engine. So in this case /help will be rewritten internally as /help/index and therefore calling the index() method of the HelpController class defined.

If more rules are matching for the same url, the one with the longest key will be used. Example:

<?php die(); ?>
/help/games|/help/games/latest|normal
/help|/help/index|normal

Only the first rule will be considered, even if it's defined before the second one.

Every other parameter added after those slashes will be handled as parameter that will be passed to the controller's method via the $args array or can be retrieved by EController public methods. See Controller page for further documentation.

Example of HelpController:

class HelpController extends EController {
    public function index($args){
        var_dump($args);
    }
}

Example of user browser opening the page www.example.com/help/index/arg1/arg2:

   {
       "arg1",
       "arg2"
   }

Every other parameter passed as standard GET like ?var1=val1&var2=val2 is correctly set on EHeaderDataParser.

This kind of parameter management works also for rewritten URLs. For example it would exactly be the same if the user calls: www.example.com/help/arg1/arg2 without the "/index" part.

This is the "normal" rewrite.


The "exact" mode rewrites a URL only if it matches exactly so additional parameters via / aren't handled and accepted. Normal GET parameters are handled as usual.

In order to build a nice title (also if it's not useful for your app logic you should use:

	
$url = ERewriter::prettify("I'm a terrible title");

Rewriting can be enabled/disabled via generic.conf.php:

<?php die("You cannot see config in here."); ?>
rewrite|yes
rewrite|no