View Source

<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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-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 &quot;Properties&quot; of &quot;Web Sites&quot;.</li>
<li>Browse to the ISAPI Filters tab and click &quot;Add...&quot;, then enter &quot;rewrite&quot; 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 &quot;rewrite&quot; 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>