James T James T - 1 month ago 7
Apache Configuration Question

Apache mod_rewrite redirect all requests for files not named x to this domain

Ok, I'm trying to achieve this:

Redirect all files of a particular filetype (except particular exceptions) to be loaded from another server, right now I'm using this rewrite rule:

RewriteEngine on
RewriteRule \.(swf|jpg|png)$ http://example.com%{REQUEST_URI} [NC,R=302,L]


And it will redirect all the swf,jpg, or png files to another hostname to serve them.
So how can I add exceptions to this rule for specific files? I've tried adding:

RewriteCond %{REQUEST_URI} ^!(log22.swf|packer.swf|sos.swf|aeVisual.swf|newBubbleSystem.swf|e.swf)$


But when I did this it completely stopped the rule from doing anything at all to the other files. Any ideas? I'm a newbie at using mod_rewrite.

Answer

You have to preprend ! to the condition's regex:

RewriteCond %{REQUEST_URI} !^(log22.swf|packer.swf|sos.swf|aeVisual.swf|newBubbleSystem.swf|e.swf)$

Note that ! and ^ are in different order than in your code, as you can negate the expression, but adding the ! before.

The reason the rule does not work is that it does never match.