29

Given these conditions (I know what they mean/do):

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d 

What does the first rule do? What is that lonely dash for?

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

I've been using this for quite some time now in combination with the Zend Framework, but I never really got what the first rule does exactly.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106

1 Answers1

54

The RewriteCond directive just describes an additional condition for a RewriteRule directive. So RewriteCond must always be associated with a RewriteRule.

In your case the three RewriteCond probably belong to the first RewriteRule like this:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

Now this rule is applied if the pattern of the RewriteRule matches the current request URL (per-directory path stripped before) and if the condition is fulfilled.

In this case the condition is only true if when mapping the request URL to the filesystem it matches either an existing file with the file size greater than 0 (-s), or a symbolic link (-l) or a directory (-d). So your rule will be applied for any URL (^.*$ matches anything) that can be mapped to something existing in your filesystem. The substitution - just means to not change anything. And the NC (no case, case insensitive, useless in this context) and L (last rule if applied) are flags that modify either the pattern, replacement or the execution of the rule.

Benoît Latinier
  • 2,062
  • 2
  • 24
  • 36
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Aaahh yes, that makes sense. Very clear explanation. Thank you. – Decent Dabbler Jan 20 '10 at 15:02
  • So what is the purpose of not changing it? Is the combination of the 2 rewrite rules saying "if it exists, show it, otherwise forward everything else to index.php" – Pete Oct 20 '14 at 10:57
  • 2
    @WillshawMedia Yes, the mentioned rule basically leaves any request to an existing file untouched and exits the rewrite process. So in combination with the other rule: rewrite only any not existing file to *index.php*. – Gumbo Oct 20 '14 at 14:49
  • What would RewriteCond %{REQUEST_FILENAME} -f be? – Wisely Wong Apr 04 '16 at 04:48