2

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]
Mudassar Bashir
  • 2,542
  • 2
  • 14
  • 11

2 Answers2

1

Well, you explicitly exclude rewriting of requests to files that do exist with this condition:

RewriteCond %{REQUEST_FILENAME} !-f

I actually I am not sure if you really want to redirect all clients for every single image to another domain name. That means you double the http requests required for the client.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • when i removed ' %{REQUEST_fileNAME} !-f ' the server goes to infinite loop. please suggest me complete code – Mudassar Bashir Dec 13 '12 at 13:22
  • i want to redirect all hotlinked images to the dummy image. (original and dummy images are on same website) – Mudassar Bashir Dec 13 '12 at 13:36
  • The endless loopp probably is a result of the fact that you redirect to a file `aa.jpg`. But you rewrite all requests to files ending with `.jpg`... That is an endless loop. Maybe you want to add an expection from rewriting for this file. – arkascha Dec 13 '12 at 14:19
1

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]
Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37