Added by Richard, last edited by Matthew Ratzloff on Dec 06, 2006  (view change)

Labels

 

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")

With lighttp use :

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

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

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

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]

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...

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.