Skip to end of metadata
Go to start of metadata

Configuring Your URL Rewriter

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:
mod_rewrite mod_rewrite Delete
htaccess htaccess Delete
url url Delete
rewriting rewriting Delete
virtualhost virtualhost Delete
iis iis Delete
isapi_rewrite isapi_rewrite Delete
lighttpd lighttpd Delete
iirf iirf Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Nov 17, 2006

    With lighttp use :

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

  2. Feb 07, 2007

    Things need to Check for successful URL Rewriter working.

    1) First Check in httpd.conf file to make sure mod_rewrite is enabled
    LoadModule rewrite_module modules/mod_rewrite.so

    2) If you are using .htaccess file,
    a) make sure "Allowoverride" is set to "All" for particular dir
    b) check what "AccessFileName" is set to (it defaults to .htaccess)
    In Windows Environment if you having trouble creating . files.... then simply change "AccessFileName" configuration file to "htaccess" or "htaccess.txt"
    (for more details http://apache-server.com/tutorials/ATusing-htaccess.html)

    3) Then Restart Apache

    If its still not working, make sure ZendFramework Library is in php include_path. Change php.ini configuration file to add that. Alternatively you could set that using set_include_path in index.php also

  3. Feb 20, 2007

    I had problems with Zend Studio Debugger on IIS+IIRF. When I inserted a simple rewrite rule like this:

    RewriteRule ^/project/.*$ /project/index.php [L,I,U]

    the debugger would time out and would not connect. After adding a higher priority rule which passes on the debugger query string (i.e. ?start_debug=1&debug_port=10000...) the debugger works ok. The two rules which work for me are:

    RewriteRule ^/project/(.)?(start_debug=1.)$ /project/index.php?$2 [L,I,U]
    RewriteRule ^/project/.*$ /project/index.php [L,I,U]

    The first rule matches for debugging, the second one for normal use without debugger.

    Configuration:
    Windows XP with IIS 5.0
    Zend Core 2.0 Beta for Windows
    Ionic ISAPI Rewrite Filter 1.2.10
    Zend Studio 5.5.0a
    Zend Framework 0.7.0

    1. Feb 21, 2007

      The first rule got somehow corrupted in my comment. The correct rule is:

      RewriteRule ^/project/(.*)?(start_debug=1.*)$ /project/index.php?$2 [L,I,U]

  4. Oct 19, 2007

    You also need another handler for lighttpd to capture query strings (and to get Zend Debugger working). Here is how I have my configuration set up:

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

    Hope this helps...

  5. May 29, 2008

    I have an alternative configuration to Apache HTTP Server. (In Centos 5 works like this)

    Create /etc/httpd/conf.d/zfapp.conf

        Alias /zfapp /usr/share/zfapp/public

        <Directory "/usr/share/zfapp/public">
            AllowOverride All
            Order Deny,Allow
            Allow from all
        </Directory>

    In /usr/share/zfapp create the structure
        application
           bootstrap.php
           controllers
           views
           models
        library
            Zend
        public
            .htaccess
            index.php

    htaccess contains:
    RewriteEngine On
    RewriteBase /zfapp/
    RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

    Everithing worked ok following the quickstart instructions

    http://myserver/zfapp
    http://myserver/zfapp/index
    http://myserver/zfapp/index/index

    All render the same view as expected.

  6. Feb 10, 2011

    Here are some additional tips on rewriting I use in .htaccess on my Swedish site http://densistavilan.se/ :

    Exclude directories
    To exclude a whole directory (for example a Wordpress installation) from from routing to index add the following before last rule:

    Redirect old URL/pages


    Make sure SEO links to one domain
    In this case we want www to redirect to the domain without www: