G'day, Have got a somewhat complex htaccess request, I thought I had the solution but it has a downfall. I have a site that gives out graphics via URL like so: <a href="http://www.site1.com"><img src="http://www.site1.com/image.jpg" /></a> Now, On the sites that hotlink the images, a nice picture shows up, and if someone clicks it it takes them to site1.com. What I'm trying to do is, get it so it reffers the click to http://www.site2.com when they come from a certain site. Here's the code I have now: ErrorDocument 404 http://www.linkingsite.com RewriteEngine On RewriteCond %{HTTP_REFERER} linkingsite\.com [NC] RewriteRule .* http://www.site2.com [L] I put that in the htaccess of site1.com, and I thought I had it all working. But it turns out that this also stops the images from loading on linkingsite.com. Is there a way I can set a rule so it allows the images to still load? Thanks for any help in advance. I've scoured google, and all I could come up with is the code I have now.
Add another rewritecond that checks if its an image - i.e. RewriteCond %{REQUEST_FILENAME} !(jpg|jpeg|png|gif)$
The code you have currently: RewriteEngine On RewriteCond %{HTTP_REFERER} linkingsite\.com [NC] RewriteRule .* http://www.site2.com [L] Line 1: Turns the url rewriting module on (self-explanatory) Line 2: Rewrite condition. The rewrite rule that follows a rewrite condition (or a number of rewrite conditions) will only be used if all rewrite conditions are met. So far you have a single condition - this is met if the referrer matches "linkingsite.com" so that the rewriterule that follows is only used when the referrer is from a page on linkingsite.com Line 3: RewriteRule follows the syntax "rewriterule PATTERN DESTINATION". This pattern matches anything (the period is a symbol for any character, * means repeated 0 or more times) and sends all requests to site2.com Now this works fine except when images are displayed - when an image on a page of the linkingsite.com is requested, the referrer will be given as linkingsite.com and this rewriterule still applies - it tries to redirect you to a different webpage so the image can't load. To get round that, you can add in another condition so your rewrite only applies if the referrer is linkingsite.com and the request is not for an image. We can do this by adding in RewriteCond %{REQUEST_FILENAME} !(gif|jpg|jpeg|png)$ REQUEST_FILENAME is a server variable containing, surprisingly enough, the name of the requested file - we then match that against a simple regex pattern (gif|jpg|jpeg|png) means gif or jpg or jpeg or png and you can add in any additional image formats you might use. The $ symbol means the end of the string. The ! inverts the expression. So basically, we're match the filename to end in an image extension and only if it does not match (because of the !) do we continue with this rewrite set and execute the rewrite rule at the end which does the redirect. ErrorDocument 404 http://www.linkingsite.com RewriteEngine On RewriteCond %{HTTP_REFERER} linkingsite\.com [NC] RewriteCond %{REQUEST_FILENAME} !(jpg|jpeg|png|gif)$ [NC] RewriteRule .* http://www.site2.com [L,R] Code (markup): Hopefully that makes some sorta sense.