Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

URL Rewriting

Apache HTTP Server

All examples that follow use mod_rewrite, an official module that comes bundled with Apache. To use it, mod_rewrite must either be included at compile time or enabled as a Dynamic Shared Object (DSO). Please consult the Apache documentation for your version for more information.

Rewriting with VirtualHost

Here is a very basic virtual host definition. These rules direct all requests to index.php, except specified file types (.js, .ico, etc.):

<VirtualHost my.domain.com:80>
    ServerName   my.domain.com
    ServerRoot   /path/to/server/root/
    DocumentRoot /path/to/server/root/my.domain.com/www

    RewriteEngine off

    <Location />
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
    </Location>
</VirtualHost>

Note the forward slash preceeding 'index.php'. These rules differ from .htaccess rules in that respect.

Rewriting with .htaccess

Routing requests

Again, these rules direct all requests to index.php, except specified file types:

RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php

Handling file and directory exceptions

These rules (used immediately prior to the RewriteRule above) exclude real files and directories from the rewriting and lets them pass through unaffected:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

You can also simply allow a specified group of files to pass through unaffected by using this line:

RewriteRule  ^(foo|bar).*  - [L]

In this case, files foo.* and bar.* will be accessed normally.

For more information, see Jayson Minard's Blueprint for PHP Applications: Bootstrapping.

Microsoft IIS

A rewriting engine does not come standard with IIS. If you haven't done so already, you will have to download and install one.

Rewriting with ISAPI_Rewrite

Helicon Tech produces the established ISAPI_Rewrite in Full and Lite versions. Currently, Full is $99, while Lite is free; however, Lite constrains you to one httpd.ini file per server.

To enable rewrite support for Zend Framework, simply add the following to httpd.ini:

[ISAPI_Rewrite]

RepeatLimit 20
RewriteRule (?!\.(js|ico|gif|jpg|png|css|swf))$ index.php

If your application is contained in a subdirectory, make sure to also set the correct RewriteBase.

To learn more about the ISAPI_Rewrite and its syntax, visit the ISAPI_Rewrite documentation.

Rewriting with IIRF

IIRF (Ionic's ISAPI Rewrite Filter) is a relative newcomer, having been in development for two years. It supports most of the same features as ISAPI_Rewrite.

For IIRF, do the following:

  1. Download and extract the files.
  2. Copy IsapiRewrite4.dll and IsapiRewrite4.ini to C:\Inetpub.
  3. Edit IsapiRewrite4.ini so it contains the following:
    RewriteRule ^(/[^.]+)$ /dispatch.fcgi?$1
  4. From the IIS Control Panel, select "Properties" of "Web Sites".
  5. Browse to the ISAPI Filters tab and click "Add...", then enter "rewrite" for the filter name.
  6. Browse to C:\Inetpub and double-click IsapiRewrite4.dll
  7. Click OK.
  8. The "rewrite" filter should now be listed at the end of the ISAPI filters list.

Lighttpd

url.rewrite-once = (".*\.(js|ico|gif|jpg|png|css)$" => "$0", "" => "/index.php")
Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.