Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is my question

My simple regex is working just fine, i just want to make sure that the first part of the regex short circuits for efficiency, i put my questions inline

1- What this thing is supposed to do is, the files with the listed extensions should not be re-written if the file exists, if the file does not exist, we rewrite the request to our php file, all other file types are always rewritten, regardless of a file's existence.

  • First condition, does the file have any of the following extensions?

For example:

RewriteCond %{REQUEST_FILENAME} \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$
  • Second condition, if the file is one of our extensions above, does it exist on the file system?

  • The evaluation of this statement is costly and I want to make sure that we short circuited on the previous RewriteCond and that this will only be checked for value if the above evaluates to true.

  • also need to know if the operating system or apache will cache the result of the file's existence (for example php does cache is_file() results)

For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ my_index.php?q=$1 [L,QSA]
  • Directories and files to be left alone.... since if they passed
  • the file existence test, they should not be rewritten at all

For example:

RewriteRule \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$ - [L]
RewriteRule ^robots.txt$ - [L]
RewriteRule ^(.*)$ my_index.php?q=$1 [L,QSA]

share|improve this question

No, it will parse each of your rewrite rules, of each type, in turn. The regex may short-circuit or not depending on the libpcre, but each RewriteRule's output is fed into the next rule in the list. From the docs:

Rewrite rules are applied to the results of previous rewrite rules, in the order in which they are defined in the config file.

However, one or more RewriteCond rules can exist before a RewriteRule, and these actually will short-circuit. So, if the statements appear in the file in the order you have them:

RewriteCond %{REQUEST_FILENAME} \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ my_index.php?q=$1 [L,QSA]
  1. The extension will be checked; if it is not in that list the next RewriteRule will not be executed and all the RewriteConds before it (the expensive one in particular) will not be checked.
  2. Otherwise, the expensive filesystem RewriteCond will be evaulated; if it is false, the same thing happens as above.
  3. If true, the URL is rewritten.
share|improve this answer
So let us recap quick 1- the expensive condition that will cause seek time will not be evaluated unless necessary 2- Apache or Linux will cache the file existence result in memory ? i think you missed this one 3- It is more efficient to rewrite the whole thing replacing the extra rules with conditions like the rewrite rules i will post in a second response here – user158369 Feb 11 '13 at 7:36
This is the new one ` RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} \.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ st_index.php?q=$1 [L,QSA] RewriteCond %{REQUEST_FILENAME} !\.(php|gif|jpg|png|ico|swf|flv|avi|mpg|jpeg|gz|ram)$ RewriteCond %{REQUEST_FILENAME} !^style.css$ RewriteCond %{REQUEST_FILENAME} !^datastor(.*) RewriteCond %{REQUEST_FILENAME} !^robots.txt$ RewriteCond %{REQUEST_FILENAME} !^servex(.*) RewriteRule ^(.*)$ bc_index.php?q=$1 [L,QSA] – user158369 Feb 11 '13 at 7:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.