<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mariano Peterson &#187; PHP</title>
	<atom:link href="http://petersonpages.com/mariano/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://petersonpages.com/mariano/blog</link>
	<description>Software engineer. Cycling fanatic. Snowboarder. Surfer. And very proud father of two boys.</description>
	<lastBuildDate>Thu, 08 Oct 2009 05:42:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Fun with Rewrite rules and Zend Framework</title>
		<link>http://petersonpages.com/mariano/blog/2009/04/25/fun-with-rewrite-rules-and-zend-framework/</link>
		<comments>http://petersonpages.com/mariano/blog/2009/04/25/fun-with-rewrite-rules-and-zend-framework/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 06:21:30 +0000</pubDate>
		<dc:creator>Mariano</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://petersonpages.com/mariano/blog/?p=28</guid>
		<description><![CDATA[I ran into a problem with Apache&#8217;s mod_rewrite module this afternoon while setting up a Zend Framework web application. I had just setup a virtual host for my app and was setting up rewrite rules to forward requests to ZF&#8217;s front controller. However, I wanted requests for existing filesystem resources (such as CSS, JS, and [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem with Apache&#8217;s <a href="http://httpd.apache.org/docs/2.2/rewrite/" title="Guide to Apache mod_rewrite">mod_rewrite</a> module this afternoon while setting up a <a href="http://framework.zend.com" title="Overview of Zend Framework">Zend Framework</a> web application.  I had just setup a virtual host for my app and was setting up rewrite rules to forward requests to ZF&#8217;s front controller.  However, I wanted requests for existing filesystem resources (such as CSS, JS, and image files) to be served directly by Apache, bypassing the Zend Framework.</p>
<div>&nbsp;</div>
<p>Loosely following the <a title="Zend Framework Quick Start Guide" href="http://framework.zend.com/docs/quickstart/create-a-rewrite-rule">ZF Quick Start Guide</a>, I copied these rules into my VirtualHost configuration:<br />
<span id="more-28"></span></p>

<div class="wp_syntax"><div class="code"><pre class="httpd" style="font-family:monospace;">&lt;VirtualHost *&gt;
    ...
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ /index.php [NC,L]
    ...
&lt;/VirtualHost&gt;</pre></div></div>

<p>These rewrite rules didn&#8217;t work for me.  They basically state, &#8220;If the requested file exists and is greater than 0 bytes, or if the requested file is a symbolic link, or if the file is a directory, then don&#8217;t rewrite the http request and don&#8217;t process any more rewrite conditions or rules for this http request. Otherwise, forward the http request to /index.php&#8221;.  The logic seemed correct, but requests for existing files were still being forwarded to the ZF front controller (/index.php).</p>
<p>To troubleshoot the problem, I enabled RewriteLog and traced its output.  I started at log level 1 and worked my way up to level 4, where I finally found the problem. You can enable the RewriteLog by adding these lines to your VirtualHost configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="httpd" style="font-family:monospace;">RewriteLog      /var/log/apache2/dev-rewrite.log
RewriteLogLevel 4</pre></div></div>

<p>The following is the RewriteLog output for one http request:</p>

<div class="wp_syntax"><div class="code"><pre class="apache_log" style="font-family:monospace;">[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (2) init rewrite engine with requested uri /tests/info.php
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (3) applying pattern '^.*$' to uri '/tests/info.php'
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (4) RewriteCond: input='/tests/info.php' pattern='-s' =&gt; not-matched
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (4) RewriteCond: input='/tests/info.php' pattern='-l' =&gt; not-matched
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (4) RewriteCond: input='/tests/info.php' pattern='-d' =&gt; not-matched
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (3) applying pattern '^.*$' to uri '/tests/info.php'
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (2) rewrite '/tests/info.php' -&gt; '/index.php'
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (2) local path result: /index.php
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (2) prefixed with document_root to /www/home/velissimo/web/index.php
[&lt;date&gt;][&lt;host&gt;][&lt;rid&gt;] (1) go-ahead with /www/home/velissimo/web/index.php [OK]</pre></div></div>

<p>The problem became apparent in the three lines printed for log level (4): mod_write was checking if the file &#8220;/tests/info.php&#8221; existed.  That path was incomplete, missing the DocumentRoot prefix (&#8220;/www/dev/web/&#8221;).  The path I expected mod_rewrite to check was: /www/dev/web/tests/info.php</p>
<p>And so I learned this lesson about mod_write:</p>
<p>In <em><a href="http://httpd.apache.org/docs/2.2/mod/directive-dict.html#Context">VirtualHost context</a></em>, %{REQUEST_FILENAME} does not expand to the full path of the requested resource as the docs indicate. Instead, it expands to the path of the resource relative to the VirtualHost&#8217;s DocumentRoot. This might be a bug, since the <a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond" title="Apache documentation for RewriteCond and REQUEST_FILENAME.">Apache documentation</a> is pretty clear that %{REQUEST_FILENAME} should expand to <em>&#8220;The full local filesystem path to the file or script matching the request.&#8221;</em></p>
<p>Interestingly, %{REQUEST_FILENAME} expands to the full path when used in .htaccess context.</p>
<p>I should point out that the Zend Framework Quick Start Guide&#8217;s suggested rewrite rules were correct. The rules failed for me because I used them in VirtualHost context, while the ZF Guide called for them to be used in .htaccess context.  When I moved the rules from my virtual host configuration to an .htaccess file, the rewrites worked correctly.</p>
<p>Still, I preferred to define the rules in the virtual host configuration.  I was able to achieve this by changing the rewrite rules to:</p>

<div class="wp_syntax"><div class="code"><pre class="httpd" style="font-family:monospace;">&lt;VirtualHost *&gt;
    ...
    RewriteEngine   on
    # The leading %{DOCUMENT_ROOT} is necessary when used in VirtualHost context
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -s [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -l [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
&nbsp;
    RewriteRule ^.*$ /index.php [NC,L]
&lt;/VirtualHost&gt;</pre></div></div>

<p>These rules worked for me on:</p>
<ul>
<li>Apache 2.2.9 (single architecture 32 bit binary) running on OSX 10.5.6</li>
<li>Apache 2.0.54 (64 bit binary) running on Red Hat Enterprise Linux ES release 4.</li>
</ul>
<p>I hope this helps save you some time or shed light on why Zend Framework&#8217;s rewrite rules aren&#8217;t working for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://petersonpages.com/mariano/blog/2009/04/25/fun-with-rewrite-rules-and-zend-framework/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

