How do I reset Apache's RewriteCond directive?

I have some Apache configuration like this:

# Image hotlinking rule
RewriteCond %{HTTP_REFERER} ^http://badsite\.com/
RewriteRule .*/static/artwork/.*\.(jpg|png)$ /static/images/hotlink.png [L]

# Redirects
RewriteRule ^static/artwork/(.+)$    http://cdn.mysite.com/artwork/$1    [L,R=301]

# CodeIgniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

It should do the following:

  1. If a request for an image comes from badsite.com, block it and display the hotlink image instead.
  2. If a regular request for an image comes in, redirect to the image hosted on the cdn subdomain.
  3. Otherwise, if the request is not a file or directory, load a web page page (goes through CodeIgniter).

However, I think the first RewriteCond is applying to the redirect rule, not the hotlinking rule, because requests for images from that referrer are redirected to the CDN, not the hotlinking image. An example in the Apache docs (bottom of the section) implies that a line break stops RewriteCond applying, but that's not happening.

Is there a way to "reset" the RewriteCond rule, or is there some other error in my config?


Line breaks are completely inconsequential; a RewriteCond only ever applies to one RewriteRule; the one directly below it.

Providing more information about your configuration would be helpful, as context is relevant in Apache. But, from the fact that your second RewriteRule works, it seems that this configuration is either set in a <Directory> context or an .htaccess file, and that /static is at the root of that context.

Given that, you first RewriteRule will never match. Change your pattern..

.*/static/artwork/.*\.(jpg|png)$

..to..

^static/artwork/.*\.(jpg|png)$