views:

305

answers:

1

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?

+2  A: 
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
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
I looked at the Apache logs, and it appears that it is attempting to access MyDomain.com/{filename.gis}/Shared/
Shawn
Had a minor typo in the RewriteRule, fixed it. Checking the code on my server now to be sure.
Curtis Tasker
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
That did the trick, thank you.
Shawn