2008年9月30日星期二

Re: [fw-mvc] OT - Rewrite rule for Zend

-- Gordon Ross <gr306@ucs.cam.ac.uk> wrote
(on Tuesday, 30 September 2008, 12:06 PM +0100):
> I'm trying to understand that Apache re-write rules, necessary to support
> Zend ('cause I want to make changes to support having some URL paths that
> are static and so should not get sent into Zend)
>
> The recommended re-write setup is:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} -s [OR]
> RewriteCond %{REQUEST_FILENAME} -l [OR]
> RewriteCond %{REQUEST_FILENAME} -d
> RewriteRule ^.*$ - [NC,L]
> RewriteRule ^.*$ index.php [NC,L]
>
> The first line is self explanatory.
>
> The next three lines (the RewriteCond ones) detail the things we're going to
> re-write (files, links & directories) Fine, got no problems here.
>
> The final line says re-write to index.php (and hence force everything
> through the front controller. I can handle this.
>
> I have two problems/questions:
>
> 1) The penultimate line:
>
> RewriteRule ^.*$ - [NC,L]
>
> In a previous message (
> http://www.nabble.com/Re%3A--fw-general--Using-the-Dojo-shipped-with-ZF-1.6.
> 0---Important-Note-p19422433.html) Matthew says that it means use the
> original path.
>
> But we're in .htaccess, so the original path is implicitly used unless
> explicitly overridden. From
> http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
>
> "However, in per-directory configuration files, the per-directory prefix
> (which always is the same for a specific directory) is automatically removed
> for the pattern matching and automatically added after the substitution has
> been done"
>
> 2) We're in .htaccess, so we'd catch access to things like /view.html, etc
> But surely it wouldn't catch /controller/action, would it ?

Let me break it down. Let's say you have the following structure in your
public directory tree:

public/
.htaccess
index.php
xmlrpc.php
js/
foo.js
css/
foo.css
static/
foo.html

The first rewrite rule, what you call the "penultimate" one, is
conditional: if the file or symlink exists with a non-zero size, we
serve it _exactly as requested_, without rewriting. What that means is
this:

http://example.com/js/foo.js -> serves js/foo.js
http://example.com/css/foo.css -> serves css/foo.css
http://example.com/static/foo.html -> serves static/foo.html
http://example.com/xmlrpc.php -> serves xmlrpc.html

If the conditions are NOT met -- i.e., no file or symlink matches, then
we drop to the *next* rewrite rule, which says to rewrite to index.php.

Does that help clarify things? The main thing to understand is that the
rules mean: "serve the file if it exists, otherwise use the ZF MVC."

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: