views:

35

answers:

2

What does this line in .htaccess do, and when would I need it?

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

I'm working with zend and I noticed the .htaccess generated by zend doesn't have this rule, but I've seen a tutorial that has it without explaining why.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
A: 

The rule:

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

basically says: "If the request does NOT (!) end ($) with .js/css/ico/giv/jpg/png redirect it to index.php".

You'd need this rule to avoid images/stylesheets and js script requests being passed to index.php.

halfdan
+3  A: 

It's a RewriteRule redirecting every request except requests for those file types (js, CSS, icons, GIF/JPG/PNG graphics) to index.php.

It's so that requests for static resources don't get handled by index.php (which is generally a good thing, because starting a PHP instance is expensive.)

It is however already handled in the code block below. This part:

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

Excludes the redirection to index.php for files that physically exist ( -s); symbolic links ( -); and existing directories (-d).

If you use the second block you quote (the one created by Zend), everything's fine.

Reference in the Apache manual

Pekka