Apache .htaccess hotlinking redirect

2019-02-15 00:07发布

问题:

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?

回答1:

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.