URL rewriting

1. Introduction

The Adventure PHP Framework enables you, to operate web pages or applications in url rewrite mode or in normal mode. URL rewriting means, that URL params are separated by "/" instead of "&" and "=". This request layout pretends, that the requested resource is located in a folder path. Another side effect is, that search engines tend to scan slash URLs more likely. Please note, that this technique does not supersede normal search engine optimization.

2. Configuration

If you use the URL rewrite mode, please be aware, that the bootstrap file (e.g. index.php) must be located in the DOCUMENT_ROOT of your webserver or any VHOST. Otherwise, the assignment of params and values can not be guaranteed.

2.1. Apache configuration

Using the URL rewrite mode a .htaccess file must be created. The following code box shows you an example, that can be used with the bigger part of your applications:

# start mod_rewrite RewriteEngine on # exclude some directories RewriteCond %{REQUEST_URI} !^(\/frontend) [NC] # define the rule itself. Every request that is not matching the exception list is # redirected to the index.php file RewriteRule !(index\.php|\.css|\.jpe?g|\.png|\.gif|\.ico)$ /index.php?apf-rewritten-query=%{REQUEST_URI}&%{QUERY_STRING} [NC,L]

Please note, that the definition


must remain unaffected. It defines the param, that is used by the PageControllerInputFilter and FrontControllerInputFilter to analyze the URL parameters and write them to the $_REQUEST-Array. The exception list

APF template

can be manipulated as desired.

Prior to release 1.15 the name of the rewrite parameter is apf-rewrited-query instead of apf-rewritten-query.
Prior to release 1.13 the name of the rewrite parameter is query instead of apf-rewrited-query.

2.2. Framework configuration

To use the framework in URL rewriting mode it is necessary to switch the registry paramater URLRewriting in the apf::core namespace to true. This can be achieved by adding
PHP code
to your bootstrap file.

3. Usage of the URL rewriting

While using the URL rewriting mode, the following hints should be kept in mind:
  • Ressources like images, css files or java scripts must be addressed absolutely.
  • Modules, written for both modes must be aware of the two flavours. If you need to knwo about the current URL layout you can retrieve this information from the registry.
  • If you are using more than one bootstrap file, please add it to the exception list in the .htaccess file.

In order to apply url rewriting without changing an application, the framework brings a output filter, that rewrites links automatically. In order to explicitly exclude links, the link attribute linkrewrite can be set to false. As of release 1.10, mailto: links are excluded by default.

Besides, the link generation mechanism introduced in release 1.14 can be used to directly format links using the desired layout.


Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.