Enable javascript for live chat

Knowledge Base Overview

Execution order

Share on Tumblr

If you are looking for specific rewrite rules with the Zend Framework please see see this other Apache Configuration document.

Is your RewriteRule in your Apache's .htaccess configuration file not working as expected?

Your httpd web server daemon may ignore the rewrite rule because you don't follow the rules of execution. In such a case it works perfect but your rules simply don't apply, they are ignored. Your logic in the files execution is ... well, illogical.

Looking around forums I see this problem a lot and I think a lot of people don't really understand how the .htaccess config file works. Sometimes they ignore or don't seem to understand the concept of execution order. Unfortunately this is crucial for the htaccess file to work—or not work.

If you understand the execution order you can solve many problems with your Apache configuration like the .htaccess very easy.

Top to bottom

Any configuration file for an Apache web server is like a batch script and such scripts are executed in sequence. This sequence is from top to bottom. An .htaccess config file is therefore no different and executed from top to bottom.

Each line in a configuration file is a directive. There are also sections but the rules and how the directives within them behave are pretty much the same. See How the sections are merged for information about Configuration Sections.

Long story short, a configuration file is interpreted line by line from top to bottom by the Apache server daemon—that's httpd on a Linux system.

Exit now!

What is also important to know here is that certain rules cause an immediate exit.

If the rule defines an external redirect the server will perform that redirect immediately and all following rules are therefore ignored. Just think about it. If your directive orders a redirect it does not make any sense to read and execute any other conditions or rules.

If this redirect leads back to the same server and configuration file then it is just a new game with the rules! In a perfect world that previous redirect rule does not apply anymore it is on to the next rule. In a not so perfect world where the rule still applies—which is unfortunately your fault and not Apache's—you end up in a loop.

There is a similar thing with a RewriteRule that matches and has the [L] flag, either alone or in combination with another flag. The [L] flag stands for Last as in This is our [L]ast rewrite rule or Stop the rewriting process here and don't apply any more rewrite rules. This last sentence is a quote straight from the Apache mod_rewrite documentation. Note that this will not immediately end the processing of the configuration, though. The [L] is flag for rewrite rules and only applies to the rewrite rules.

However, the RewriteRule also have a [R] flag and stands for [R]edirect to the mentioned path. You will usually use and find these two [L,R] flags together. If the condition in the rewrite true is true then the redirect will happen immediately and the [L]ast flag is actually implied.

Just note that for a redirect the [L] flag alone does nothing for you.

Be logical

Now you only have to pretend to be Captain Spock and be logical. Simply apply some logic in your configuration file which is mainly taking care of the order for your directives.

Any request for a redirect should be fairly early in the script. It does not matter in this case if the redirect is caused by any of the Redirect directives or a RewriteRule (see mod_rewrite link above). If that condition is met the execution will stop and send a redirect response to the browser. The browser will send a new request and we have a new game.

If you use SetEnv for environment variables you don't have to set them before any redirects. If a redirect applies the environment variable has been set for nothing. Although just a millisecond it is really a waste of your server time.

The last resort

If you work with the Zend Framework you will learn and copy from the manual some RewriteRule to an index.php file.

Such a RewriteRule to an index.php is something we will add very late at the end and bottom of the .htaccess file.

Just think of it as your last resort—like an if all fails then rule. It does not matter if this is the Zend Framework or any other application you funnel through an index.php or other script for that matter. You can add many RewriteCond directives and send them to specific files. Just don't forget to add the [L] flag. If the RewriteCond is true and you don't have the [L]ast flag the execution of the configuration goes on and may result in an unwanted result.

blog comments powered by Disqus