Merging ZEND framework with an existing site - mod_rewrite

In a typical application of the Zend framework, a RewriteRule is created to route all non resources to the front controller.

As I read the documentation I’m wondering how this can be implemented in an existing web site that is littered with ye olde style scripts that mix-up the Model, View and Controllers within hardcoded html pages.

mod_rewrite

If a files already exists can it be left to do its stuff without watching the request get whisked away by mod_rewrite to end up cavorting with that slick Front Controller?

This tutorial is also quite a good intro into the whole scary business of mod_rewrite. The penultimate page mentions that ‘conditions’ can be used and they can access a REQUEST_FILENAME variable (amongst others). So it seems that the tools are there for me. I’ve just got to work this one out.

I’ve been searching and, although it is not a solution, the “Search pages in more than one directory” code that i found in this URL Rewriting Guide might offer a clue.

My ideal rewrite rule:

If (script exists )
{
//e.g matches the request and its canonical siblings
Let it go and be free - no rewrite needed
}
elseif (request does NOT point to a special folder )
{
Let it go and be free - no rewrite needed
to show a 404
}
else
{
Send it to Front controller and do fancy stuff
}

Rewrite Conditions that Check for Regular Files

Wow - its amazing how many ways you can describe “a regular file”. I’ve been tearing my hair out searching for Rewrite conditions that look for “static pages”, “existing scripts”, “actual pages”, “hardcoded pages” etc. - The phrase to use is “regular file”.
Here is the lowdown on this for Apache 1.3. (Comparable page for Apache 2.2)

I can add another key to my quest for this knowledge:

‘-f’ (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
‘-d’ (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.

I think I’ve cracked it!

Ok - so after some hacking away - I have created some rules in the .htaccess to help me test out the Zend framework on my live site.
I also, hope that when it comes to Zendifying more pages it should not be too difficult to add those REQUEST_URIs onto the last RewriteCond using an [OR]

<IfModule mod_rewrite.c>
RewriteEngine On

#im not sure if this next line does anything worthwhile
RewriteBase /testzend/

#if REQUEST_FILENAME is not a regular file
RewriteCond %{REQUEST_FILENAME} !-f

#and if REQUEST_FILENAME is not a directory
RewriteCond %{REQUEST_FILENAME} !-d

#and the requested resource IS in the /testzend/ folder
#Note to future self: As i convert site to zend framework
#i could add other file and folder patterns
#here using an [OR]
RewriteCond %{REQUEST_URI} ^/testzend/.* [NC]

RewriteRule !\.(js|ico|gif|jpg|png|css)$ /testzend/index.php [L]

</IfModule>

Update: After all that scrabbling about the net trying to find an answer I discover this page, which offers an example rewrite rule that looks similar to the one i wanted in the first place!

Leave a Reply