Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
are you trying to redirect only from badsite.com? or from many sites? –  ThinkingMonkey Jan 29 '12 at 18:56
    
No, I want to show hotlink.png when the referer is badsite.com, and redirect otherwise. –  DisgruntledGoat Jan 29 '12 at 19:50

1 Answer 1

up vote 4 down vote accepted

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)$
share|improve this answer
    
You're right, the rules were in a Directory context, your correction fixed it, thanks! –  DisgruntledGoat Jan 29 '12 at 21:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.