RewriteCond - want to ignore certain files - how?

Topics: Developer Forum
Jul 10, 2008 at 4:44 PM
Hi All,

I'm trying to write a condition that excludes certain file types like images, javascript files etc.

Here's what I have but it's not ignoring these file types. Is my regex crappy or should I be using a different server var?

RewriteCond %{REQUEST_FILENAME}                 (?!.*\.css|*\.jpg|*\.gif|*\.js|*\.doc)$   [I]

Also,

I'm trying to write a condition so my rules ignore anything at specified path:

RewriteCond %{PATH_INFO}    !^/admin/.+$      [I]

Again, is it sloppy regex or am I using the wrong server var?

Thanks for your help.
Jul 10, 2008 at 5:24 PM
You're using a Conditional rule... once you have your rule results you need to do something with the condition. If it's true/false... do something. It needs to be married with a RewriteRule.

What are you trying to accomplish with your rule? Just redirect someone away from a location?
Jul 10, 2008 at 5:51 PM
Ok I figured out how to "ignore" a path:

RewriteRule ^/admin/(.*)$     /admin/$1     [I,L]

But still not sure how to exclude certain files...
Jul 10, 2008 at 5:55 PM


mcwilliams132 wrote:
You're using a Conditional rule... once you have your rule results you need to do something with the condition. If it's true/false... do something. It needs to be married with a RewriteRule.

What are you trying to accomplish with your rule? Just redirect someone away from a location?

MC -

Thanks for responding.

I've got a set of rules below the conditional statement that I didn't include in my example.

The rules below it work just fine but the rules are run against every file that is called from this site. And to reduce the "overhead" on the server I'd like the tell the filter to ignore images, scripts, css, etc.

Make sense?
Coordinator
Jul 10, 2008 at 7:44 PM
Edited Jul 10, 2008 at 7:45 PM

YES, what you are doing makes sense.

The rules are evaluated in a sort of waterfall, so if you can handle the more common URLs in the rules defined earlier in your INI file, then you will have better performance and efficiency in the operation of the filter.

The rule you have...

RewriteRule ^/admin/(.*)$     /admin/$1     [I,L]

...Will rewrite any URL that comes in, with /admin as a first segment in it.  It will rewrite it to exactly the same URL. 
You know that the filter recurses: the output of a rule is then passed through the ruleset again.  But you've stopped that with the [L] flag.

If you also want to ignore certain files, or files with particular extensions, then you can do something like this:

RewriteRule ^/(.+\.(css|jpg|js|gif))$     /$1     [I,L]

This will fire on any URL that ends with .css or .jpg or .gif. It fires on URL requests such as:
http://server/banner.gif
http://server/style/basic.css
http://server/script/precheck.js

Keep in mind it will also fire on URL requests in which the query string ends in a pattern, such as these:
http://server/page.aspx?query=banner.gif
http://server/index.php?basic.css
http://server/default/?q=script/precheck.js

If you want to avoid THAT, then you need to filter out the question mark, such as with:

RewriteRule ^/([^\?]+\.(css|jpg|js|gif))$     /$1     [I,L]

This says, any path that has no question mark, and ends in a .css .jpg .js or .gif, should be rewritten to itself and then STOP.