Search code examples
.htaccessmod-rewritehttp-redirecthotlinking

mod_rewrite not working properly for hotlinked images


i want to redirect all hotlinked images to one dummy image.

i applied htaccess code for this purpose, but it is only redirecting 404(not found) images

means the hotlinked images with correct links are not being redirected.

i also have applied a redirect for non-www to www version, think this redirect is causing some conflict with hotlinked redirect code

here is the code

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f

RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite/.*$ [NC]
RewriteRule \.(gif|jpg|png) http://www.mysite.com/temp/aa.jpg [R,L]

Solution

  • A modification like this one should work:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^mysite\.com$
    RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^.*/aa.jpg$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com/?$ [NC]
    RewriteRule ^.*\.(gif|jpg|png)$ http://www.mysite.com/temp/aa.jpg [R=301,L]