PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Introduction

Zend_Controller_Router_Rewrite is the standard framework router. Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request. This values of the module, controller, action and other parameters are packaged into a Zend_Controller_Request_Http object which is then processed by Zend_Controller_Dispatcher_Standard. Routing occurs only once: when the request is initially received and before the first controller is dispatched.

Zend_Controller_Router_Rewrite is designed to allow for mod_rewrite-like functionality using pure PHP structures. It is very loosely based on Ruby on Rails routing and does not require any prior knowledge of webserver URL rewriting. It is designed to work with a single Apache mod_rewrite rule (one of):

<?php
RewriteEngine on
RewriteRule 
!\.(js|ico|gif|jpg|png|css|html)$ index.php

or (preferred):

<?php
RewriteEngine On
RewriteCond 
%{REQUEST_FILENAME} -[OR]
RewriteCond %{REQUEST_FILENAME} -[OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule 
^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

The rewrite router can also be used with the IIS webserver (versions <= 7.0) if Isapi_Rewrite has been installed as an Isapi extension with the following rewrite rule:

<?php
RewriteRule 
^[\w/\%]*(?:\.(?!(?:js|ico|gif|jpg|png|css|html)$)[\w\%]*$)? /index.php [I]

IIS Isapi_Rewrite

When using IIS, $_SERVER['REQUEST_URI'] will either not exist, or be set as an empty string. In this case, Zend_Controller_Request_Http will attempt to use the $_SERVER['HTTP_X_REWRITE_URL'] value set by the Isapi_Rewrite extension.

IIS 7.0 introduces a native URL rewriting module, and it can be configured as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
     <system.webServer>
         <rewrite>
             <rules>
                 <rule name="Imported Rule 1" stopProcessing="true">
                     <match url="^.*$" />
                     <conditions logicalGrouping="MatchAny">
                         <add input="{REQUEST_FILENAME}"
                             matchType="IsFile" pattern=""
                             ignoreCase="false" />
                         <add input="{REQUEST_FILENAME}"
                             matchType="IsDirectory"
                             pattern="" ignoreCase="false" />
                     </conditions>
                     <action type="None" />
                 </rule>
                 <rule name="Imported Rule 2" stopProcessing="true">
                     <match url="^.*$" />
                     <action type="Rewrite" url="index.php" />
                 </rule>
             </rules>
         </rewrite>
     </system.webServer>
</configuration>

If using Lighttpd, the following rewrite rule is valid:

url.rewrite-once = (
    ".*\?(.*)$" => "/index.php?$1",
    ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
    "" => "/index.php"
)

Zend Framework