Display watermark on images from with referrals from outside sources

I have an image site of sorts and often get heavy traffic, not to the pages the images are on but to the images themselves. I’ve set up a script that has created copies of the original image with a small watermark that points them to my homepage.

The problem is displaying this alternate image only when the traffic is coming from an outside source. I have the htaccess code and as far as I can tell it’s all good, but when I set it loose, I get a 500 internal service error.

This is what I have. The images are all in mydomain.com/images/ and the watermarked images are all in mydomain.com/images/watermark/. All the images are jpg’s and all have a name that is a number.


RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://([-a-z0-9]+\\.)?mydomain\\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^images/(.*)\\.jpg$ /images/watermark/$1\\.jpg [L]

The 500 error only comes on when the referrer is not my site. They display correctly when I go to the image directly for example. Am I doing anything wrong? Could this be a server configuration?

PITA,

Your problem is the lack of knowledge of regex (and using the LAZY regex as a result). Do you not think that (.) will match watermark/(.), too?

Change (.*) to ([a-z]+) or ([^/]+) and you’ll avoid the loop.

Regards,

DK

D’oh! Can’t believe I missed that.
Thanks a bunch. I owe you my second-born (I promised my firstborn to someone else–I gotta stop using my future children as payment).

PITA,

:lol: :lol: :lol:

Regards,

DK

Sooooo that worked great on that site, but as a matter of experimentation, for another site with a similar structure, I want to redirect to the page with the image on it.

The problem is that right now the images have a space in their names and I don’t know how to deal with that.

The images are in mydomain.com/images/ and have the name structure “image (#).jpg” --the # represents a number; there are about 4000 images, all numbered. The page with the image is at mydomain.com/?i=# where # is the number that’s in the image name.

Assume the first 3 lines are the same. I tried the following, and get a 500 error:


RewriteRule "^images/image \\(([0-9]+)\\)\\.jpg$" /?i=$1 [L]

Not sure what I’m getting wrong here. Guess I’m not very good at this… :frowning:

Mr PITA,

Why would anyone use quotes in a mod_rewrite statement? Escape brackets or …? Please have a look at the tutorial in my signature.

Regards,

DK

Idk I saw it in some other place that if there’s a space you should use quotes around the whole thing. I shouldn’t trust everything I read on the internet, but yea I’ll take a look at that tutorial.

Thanks

PITA,

NEVER use quotes in mod_rewrite statements!

If you have a space (i.e., %20) in a URL:

  1. Shame on you.

  2. Use [\ ] to specify the space character (to match the %20). Yes, that’s an escaped space in the character range definition.

Regards,

DK

Good God, dklyyn, you are a genius.
I bow down in praise.

PITA,

On your feet, man!

Okay, I’m glad that you got that resolved but may I recommend that you read the tutorial in my signature. I’m sure that I mentioned there that I use a str_replace(’ ‘,’_',$link) to replace spaces in the URI AND reverse that to reinstate the spaces for your db access. Example: http://wilderness-wally.com which uses (almost) ONLY titles of his articles for links. Easy-peasy!

Regards,

DK

Wonderful !