Skip to end of metadata
Go to start of metadata

<h1>Configuring Your URL Rewriter</h1>

<h2>Apache HTTP Server</h2>

<p>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 <a href="http://httpd.apache.org/docs/">Apache documentation</a> for your version for more information.</p>

<h3>Rewriting with VirtualHost</h3>

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

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
<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 %

Unknown macro: {REQUEST_FILENAME}

!-f
RewriteCond %

!-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
</Location>
</VirtualHost>
]]></ac:plain-text-body></ac:macro>

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

<h3>Rewriting with .htaccess</h3>

<p><strong>Routing requests</strong></p>

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

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
]]></ac:plain-text-body></ac:macro>

<p><strong>Handling file and directory exceptions</strong></p>

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

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
RewriteCond %

Unknown macro: {REQUEST_FILENAME}

!-f
RewriteCond %

!-d
]]></ac:plain-text-body></ac:macro>

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

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
RewriteRule ^(foo|bar).* - [L]
]]></ac:plain-text-body></ac:macro>

<p>In this case, files foo.* and bar.* will be accessed normally.</p>

<p>For more information, see Jayson Minard's <a href="http://devzone.zend.com/node/view/id/70">Blueprint for PHP Applications: Bootstrapping</a>.</p>

<h2>Microsoft IIS</h2>

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

<h3>Rewriting with ISAPI_Rewrite</h3>

<p>Helicon Tech produces the established <a href="http://www.isapirewrite.com/">ISAPI_Rewrite</a> in Full and Lite versions. Currently, Full is $99, while Lite is free; however, Lite constrains you to one <code>httpd.ini</code> file per server.</p>

<p>To enable rewrite support for Zend Framework, simply add the following to <code>httpd.ini</code>:</p>

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
[ISAPI_Rewrite]

RepeatLimit 20
RewriteRule (?!\.(js|ico|gif|jpg|png|css|swf))$ index.php
]]></ac:plain-text-body></ac:macro>

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

<p>To learn more about the ISAPI_Rewrite and its syntax, visit the <a href="http://www.isapirewrite.com/docs">ISAPI_Rewrite documentation</a>.</p>

<h3>Rewriting with IIRF</h3>

<p><a href="http://cheeso.members.winisp.net/IIRF.aspx">IIRF</a> (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.</p>

<p>For IIRF, do the following:</p>

<ol>
<li>Download and extract the files.</li>
<li>Copy <code>IsapiRewrite4.dll</code> and <code>IsapiRewrite4.ini</code> to <code>C:\Inetpub</code>.</li>
<li>Edit <code>IsapiRewrite4.ini</code> so it contains the following:
<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[RewriteRule ^(/[^.]+)$ /dispatch.fcgi?$1]]></ac:plain-text-body></ac:macro></li>
<li>From the IIS Control Panel, select "Properties" of "Web Sites".</li>
<li>Browse to the ISAPI Filters tab and click "Add...", then enter "rewrite" for the filter name.</li>
<li>Browse to <code>C:\Inetpub</code> and double-click <code>IsapiRewrite4.dll</code></li>
<li>Click OK.</li>
<li>The "rewrite" filter should now be listed at the end of the ISAPI filters list.</li>
</ol>

<h2>Lighttpd</h2>

<ac:macro ac:name="noformat"><ac:plain-text-body><![CDATA[
url.rewrite-once = (".*\.(js|ico|gif|jpg|png|css)$" => "$0", "" => "/index.php")
]]></ac:plain-text-body></ac:macro>

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

    <p>With lighttp use :</p>

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

  2. Feb 07, 2007

    <p>Things need to Check for successful URL Rewriter working.</p>

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

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

    <p>3) Then Restart Apache</p>

    <p>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</p>

  3. Feb 20, 2007

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

    <p>RewriteRule ^/project/.*$ /project/index.php <ac:link><ri:page ri:content-title="L,I,U" /></ac:link></p>

    <p>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:</p>

    <p>RewriteRule ^/project/(.<strong>)?(start_debug=1.</strong>)$ /project/index.php?$2 <ac:link><ri:page ri:content-title="L,I,U" /></ac:link> <br />
    RewriteRule ^/project/.*$ /project/index.php <ac:link><ri:page ri:content-title="L,I,U" /></ac:link> </p>

    <p>The first rule matches for debugging, the second one for normal use without debugger.</p>

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

    1. Feb 21, 2007

      <p>The first rule got somehow corrupted in my comment. The correct rule is:</p>

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

  4. Oct 19, 2007

    <p>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:</p>

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

    <p>Hope this helps...</p>

  5. May 29, 2008

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

    <p><strong>Create /etc/httpd/conf.d/zfapp.conf</strong></p>

    <p>    Alias /zfapp /usr/share/zfapp/public</p>

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

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

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

    <p><strong>Everithing worked ok following the quickstart instructions</strong></p>

    <p> <a href="http://myserver/zfapp">http://myserver/zfapp</a><br />
    <a href="http://myserver/zfapp/index">http://myserver/zfapp/index</a><br />
    <a href="http://myserver/zfapp/index/index">http://myserver/zfapp/index/index</a></p>

    <p>All render the same view as expected.</p>

  6. Feb 10, 2011

    <p>Here are some additional tips on rewriting I use in .htaccess on my Swedish site <a href="http://densistavilan.se/">http://densistavilan.se/</a> :</p>

    <p><strong>Exclude directories</strong><br />
    To exclude a whole directory (for example a Wordpress installation) from from routing to index add the following before last rule:</p>
    <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
    RewriteCond %

    Unknown macro: {REQUEST_URI}

    ^/wp/ [OR]  #Do not dispatch requests for Wordpress access
    ]]></ac:plain-text-body></ac:macro>

    <p><strong>Redirect old URL/pages</strong></p>
    <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[

    RewriteRule ^boats$ /info/boats [R=301,L] #/info/boats is the new URL
    ]]></ac:plain-text-body></ac:macro>
    <p><br class="atl-forced-newline" /> Make sure SEO links to one domain<br />
    In this case we want www to redirect to the domain without www:</p>
    <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
    RewriteCond %

    Unknown macro: {HTTP_HOST}

    !^yourdomain\.com

    RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
    ]]></ac:plain-text-body></ac:macro>