homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 43 message thread spans 2 pages: < < 43 ( 1 [2]     
Rewrite image for specific referrer
batface




msg:4515654
 8:07 pm on Nov 4, 2012 (gmt 0)

I'm hoping someone can help with this. I have a background image I want to rewrite to a different image if a specific referrer is used. The following does not trigger the rewrite.

RewriteEngine On
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^http://(www\.)?thereferrer\.com [NC]
RewriteRule ^images/image\.png$ http://www.example.com/directory/images/new-image.png [R=302,L]

The rewrite rule works by itself but the following condition and rewrite works, so where am I going wrong?

RewriteEngine On
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} ^http://(www\.)?thereferrer\.com [NC]
RewriteRule .* - [F]

 

batface




msg:4517589
 7:14 pm on Nov 9, 2012 (gmt 0)

eek, errors - should be HTTP_REFERER. Only error in post not htaccess.

phranque




msg:4517616
 9:21 pm on Nov 9, 2012 (gmt 0)

So I see the initial IP address is mine - BT Internet, then the referrer is http://www.example.com/test.html. It was originally making sense to me to use:

RewriteCond %{REMOTE_REFERER} ^http://www\.example\.com.*$ [NC]

"REMOTE_REFERER" should be "HTTP_REFERER" which will be either null or the URL of the referring page.
ending a regular expression in ".*$" is unnecessary and inefficient because it says the pattern can end in anything or nothing and you aren't even capturing that pattern.

but as with either REMOTE_HOST or REMOTE_ADDR using the IP for example.com the alternative image is not displayed.

you can't filter by referring IP address in .htaccess using mod_rewrite.

if you want to filter according to your IP address you need to look in your server access log file and see what IP address is being logged for your requests and try that IP address in your .htaccess file.

lucy24




msg:4517644
 10:59 pm on Nov 9, 2012 (gmt 0)

REMOTE_HOST should be looking for the IP of the user that you want to block not set to the IP of your website.

I assumed that he was testing on himself before putting out the real thing. So when I said "your IP" I meant his home IP. That's also what I meant by closing anchor, as in:

... REMOTE_HOST ^123\.456\.78\.90$

Obviously only makes a difference if the fourth piece is <=25 ;) but
1.1.1.1
unescaped can cover many, many addresses. Taken at face value I make it 11 aa blocks (1\. and 1\d1\.) and the same number of aabb permutations.

batface




msg:4518230
 7:34 am on Nov 12, 2012 (gmt 0)

ok going back a post, just to clarify - does this condition make sense?

86.159.xxx.xxx - - [08/Nov/2012:04:43:06 +0100] "GET /Landingpage/index.html HTTP/1.1" 200 630 "http://www.example.com/test.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"

RewriteCond %{REMOTE_REFERER} ^http://www\.example\.com.*$ [NC]

If not, why not, but please in layman's talk?

lucy24




msg:4518237
 8:10 am on Nov 12, 2012 (gmt 0)

.*$

This component is only necesssary when you need to capture all the way to the end. When matching alone, you don't need it. "There may or may not be more stuff after dot com, and eventually the name comes to an end."

If you want to constrain the referer to the specific page "/test.html" you should say so. If you're only looking at the domain, it doesn't matter.

Now, about that "/index.html" ...

:: tweet tweet, chirp chirp ::

Oh yes and: Now we're back on the html page. I thought the issue was about a background linked from the page. Or have I got my threads mixed up again?

batface




msg:4518248
 9:56 am on Nov 12, 2012 (gmt 0)

No you haven't, I may have changed the URL but the need is the same, just getting caught up in the "use example.com". To recap:

In the log there is:

86.159.xxx.xxx - - [08/Nov/2012:04:43:06 +0100] "GET /Landingpage/index.html HTTP/1.1" 200 630 "http://www.example.com/test.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"

So http://www.example.com/test.html holds a link to the landing page. I want to be able to serve a different background image for any user that is on that domain clicking through to the landing page (I have no control over that referring domain).

This rewrite rule works as I expect for any traffic hitting the the landing page:

RewriteRule ^images/image\.png$ /directory/images/new-image.png[L]

Adding the following condition to rewrite only if the referring URL is example.com does not work:

RewriteCond %{REMOTE_REFERER} ^http://www\.example\.com.*$ [NC]
RewriteRule ^images/image\.png$ /directory/images/new-image.png[L]
(whether or not I keep in the .*)

However, anything not example.com gives me the alternative image from example.com or anywhere else.

RewriteCond %{REMOTE_REFERER} !^http://www\.example\.com.*$ [NC]
RewriteRule ^images/image\.png$ /directory/images/new-image.png[L]
(whether or not I keep in the .*)

The second !example.com gives me the impression that the condition works in a broad sense, but not recognising the detail of the condition i'm using.

Using the other conditions detailed in previous posts leaves me with the same results.

Is there something else I can try?

phranque




msg:4518269
 11:04 am on Nov 12, 2012 (gmt 0)

why are you still using REMOTE_REFERER?

"REMOTE_REFERER" should be "HTTP_REFERER" which will be either null or the URL of the referring page.

lucy24




msg:4518270
 11:07 am on Nov 12, 2012 (gmt 0)

:: sigh ::

Didn't this already get hashed out on the first page?

The referer for an image is the page the image lives on. Or the CSS, in the case of a background image and a separate style sheet. Or nothing, in the case of a robotic request.

The outside referer only exists for the page itself. Look again at your logs. This time look at the requests for images. Notice how the referer, if any, is always your own site.

The "not" version of the rule works because the referer isn't example dot com. It's your own site. If the referer is example dot com, they're hotlinking. But that's a whole nother question.

why are you still using REMOTE_REFERER?

I don't think he is really. I think it's just his cat editing the posts.

Edit:
I detoured to check on my spare site. If you set a condition with {REMOTE_REFERER} -- or presumably anything else that doesn't exist -- mod_rewrite will simply behave as if the condition has failed, and won't execute the rule. I expected it to throw a 503, which would be pretty hard to overlook.


Tangential speculation: Do search engines fetch background images right along with everything else? How annoying, if so.

batface




msg:4518300
 11:44 am on Nov 12, 2012 (gmt 0)

I don't think he is really. I think it's just his cat editing the posts.


I just can't get the staff.

The referer for an image is the page the image lives on. Or the CSS, in the case of a background image and a separate style sheet. Or nothing, in the case of a robotic request.


So I can rewrite to get the alternative image for all visitors but there is no way that any kind of condition/rule can be applied for only one or more chosen domains to be served the alternative image?

phranque




msg:4518468
 10:20 pm on Nov 12, 2012 (gmt 0)


RewriteCond %{REMOTE_REFERER} ^http://www\.example\.com.*$ [NC]
RewriteRule ^images/image\.png$ /directory/images/new-image.png[L]


this should work if you use HTTP_REFERER instead of REMOTE_REFERER

g1smd




msg:4518474
 10:37 pm on Nov 12, 2012 (gmt 0)

Now we are clear what you are trying to do, the answer is to alter the code on your HTML page (hopefully it's a PHP script or similar) so that it loads /image1.png for some users and /image2.png for other users based on the PHP script detecting the referrer for the page.

If you're really clever with code, you could dynamically alter the CSS instead.

You can't do anything by detecting the referrer for the image because the referrer for the image will always be URL of the page that the image is ON.

Rememeber that the browser makes separate requests for the page, and for each of the images and files, and each one will have their own referrer data.

phranque




msg:4518505
 11:38 pm on Nov 12, 2012 (gmt 0)

what g1smd said!

i didn't read your log entry carefully and misunderstood your problem as being for the image referrer vs the referring page problem.

batface




msg:4518667
 8:07 am on Nov 13, 2012 (gmt 0)

Thanks guys, now I understand.

This 43 message thread spans 2 pages: < < 43 ( 1 [2]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved