Zend Framework rewriting rules explained (.htaccess)

blog has moved

I’ve moved my blog under my personal domain:

http://bsagols.com/blog

I didn’t do the proper redirection ’cause it costs 12 $ a year and I don’t see the point paying for this (12 $ (a year !) to put 1 line in an .htaccess file ?!).

Here is the new URL of this post:

bsagols / blog - Zend Framework rewriting rules explained (.htaccess)

Here follows the original post…

- -

Sometimes (often ?!) you just copy / paste some code without taking time to deeply understand it… I use to do this when I’m running out of time (always ?!)…

This way of doing things is not perennial… At all. The day you will be stuck because you didn’t take that time, always comes.

This is what happened with the rewriting rules of a standard Zend Framework project, I just copied / pasted them.

It worked. Until the day I got stuck. I needed to understand why some requested files were passing though the rewriting logic when others weren’t.

Let’s shade some light on these rewriting rules:

1. RewriteCond %{REQUEST_FILENAME} -s [OR]
2. RewriteCond %{REQUEST_FILENAME} -l [OR]
3. RewriteCond %{REQUEST_FILENAME} -d
4. RewriteRule ^.*$ - [NC,L]
5. RewriteRule ^.*$ index.php [NC,L]

First, let’s see what the REQUEST_FILENAME variable is:

REQUEST_FILENAMEThe full local filesystem path to the file or script matching the request.

Now let’s take the rewriting rules one by one.

1. RewriteCond %{REQUEST_FILENAME} -s [OR]

[ Source: mod_rewrite ]

-s‘ (is regular file, with size)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file with size greater than zero.

So IF the requested file name is an existing non-empty file on the file system…

2. RewriteCond %{REQUEST_FILENAME} -l [OR]

[ Source: mod_rewrite ]

-l‘ (is symbolic link)
Treats the TestString as a pathname and tests whether or not it exists, and is a symbolic link.

OR is an existing symbolic link on the file system…

3. RewriteCond %{REQUEST_FILENAME} -d

[ Source: mod_rewrite ]

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

OR is an existing directory on the file system…

4. RewriteRule ^.*$ - [NC,L]

Don’t rewrite anything !!

Let’s take a closer look at that line. Why doesn’t it rewrite anything? Because of the special character “-” (dash):

[ Source: RewriteRule directive ]

Syntax: RewriteRule Pattern Substitution [flags]

[ ... ]

Special string ‘-’ (dash) in Substitution means no substitution and is useful when you need to apply the rule while leaving original URL untouched.

This is the behavior we are expecting. If a requested file name complies with one of the above rewrite conditions, we don’t want the rewrite process to occur.

This means that any file, symlink or directory that actually exists in the /public/ folder (root of the webapp…) will be served normally by the Apache server. For instance, all the static resources (css, js, etc…) are located in the /public/ folder…

In addition, the flags [L] indicate that this rewrite rule is the last one to be applied, none will occur after that one, if processed:

[ Source: mod_rewrite ]

last|L‘ (last rule)
Stop the rewriting process here and don’t apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL (‘/‘) to a real one, e.g., ‘/e/www/‘.

So again, if that rule is processed, it will prevent the following ones in the .htaccess to occur… This flag is interesting in our case, because the following rewrite rule is very generic and we don’t want it to occur always:

5. RewriteRule ^.*$ index.php [NC,L]

This rule means rewrite / re-orient / re-map any request URI coming in (^.*$) to index.php. And as you may know the index.php contains the core logic of a standard Zend Framework MVC project.

Note: The flag [NC] means “no case”:

[ Source: mod_rewrite ]

nocase|NC‘ (no case)
This makes the Pattern case-insensitive, ignoring difference between ‘A-Z’ and ‘a-z’ when Pattern is matched against the current URL.

So what’s important here, is that this last rule applies only if the one just above (line 4.) haven’t occured. Meaning that all the previous conditions (RewriteCond) only apply to the RewriteRule at line 4.

It allows the static resources to be served normally.

Hope that helps someone, somewhere.

PS: Understanding this was not much complicated than reading the official documentation of the mod_rewrite Apache module.

Advertisement
Tagged , ,

One thought on “Zend Framework rewriting rules explained (.htaccess)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.