Q:

.htaccess to redirect jpg to a webpage

With my site, I have www.mysite.com/images/1.jpg and www.mysite.com/pixels/1.jpg
I would like for the .htaccess to redirect ANY jpg (just jpgs) image throughout my entire website to redirect to www.mysite.com/directory/
I had a .htaccess file that did this, but then I couldn't use the image on my own website like  it would just be a blank box but if I visited the exact url of the image, it took me to www.mysite.com/directory/
The reason for this is like if someone finds the images of mine on Google Images or on Tumblr or another website, once they click the image and visit the url of the image on my site, it will redirect them to a directory like www.mysite.com/directory/ 
I hope I explained this well enough.

A:

I gather you want to check the referer. It's one of the HTTP request headers that tells the webserver what URL they were at that referred them to what they're requesting. Also keep in mind that referers can be forged. You can add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?mysite\.com/ [NC]
RewriteRule \.jpg$ /directory/ [L,R=302.NC] 

