4

I am trying to create a redirection when someone hotlinks images in one directory on my site. If someone hotlinks an image, I want to redirect them to a corresponding image (same file name) in a different directory.

If someone hotlinks:

www.mydomaoin.com/PlayImages/Basic/Embedded/{ImageName.gif}

I want it to redirect to:

www.mydomaoin.com/PlayImages/Basic/Shared/{ImageName.gif}

Thoughts?

Shawn
  • 205
  • 1
  • 4
  • 9

1 Answers1

9
RewriteEngine on

#redirect image hotlinks
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/?.*$ [NC]
RewriteCond %{REQUEST_URI} (.*)/Embedded/(.*jpg|.*gif|.*png)$ [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}/%1/Shared/%2 [R=302,L]

If the referrer is not blank, and the referrer is not equal to your own domain, and the request is for a resource in the /Embedded folder ending in jpg/gif/png, then rewrite the url to replace /Embedded with /Shared

You may want to change the [R=302] to a different code to suit your needs.

Curtis Tasker
  • 11,115
  • 2
  • 23
  • 23
  • It is obviously doing something, since images from that directory is not linking a file, I'm getting a broken link (Red X). Is there a way to output to a file for debugging? – Shawn Jul 14 '09 at 17:16
  • I looked at the Apache logs, and it appears that it is attempting to access MyDomain.com/{filename.gis}/Shared/ – Shawn Jul 14 '09 at 17:32
  • Had a minor typo in the RewriteRule, fixed it. Checking the code on my server now to be sure. – Curtis Tasker Jul 14 '09 at 17:48
  • Sorry about that, I should have tested the code before posting it. There was the aforementioned typo in RewriteRule, and I used REQUEST_FILENAME in the RewriteCond when I meant to use REQUEST_URI. It tests fine now :) – Curtis Tasker Jul 14 '09 at 18:22