custom photo pages with .htaccess

korzry
korzry used Ask the Experts™
on
I'd like to create a custom web page for displaying photos.  Normally, when the web browse pointed to a .jpg or .gif file, it is displayed with the default plain background.  I'd like to change this to a custom page to match the rest of my website.

Is it possible to use the .htaccess file to redirect users to either a custom webpage or even a cgi script when the browser points to a .jpg of .gif?  I am thinking it might work similiar to an error page.

Ryan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Ryan,

It would help if you could specify what Web server, and OS platform you would be working with, and what CGI platform - perl, etc you are interested to work the solution.

cheers.

Author

Commented:
It's an Apache server on Unix.  I've been using Perl for CGI.  I currently am using a CGI script to do the custom background for displaying pictures, but need to put this into the hyperlink to run it.  I want it to run automatically when pointed to a picutes, or better yet, not require the cgi script at all.

Commented:
Ryan,

Take a look at mod_action (http://httpd.apache.org/docs/mod/mod_actions.html).  It seems that it may suit what you need.  I personally never tried it though.

hope this helps.

Author

Commented:
samri,

Adding an action to my .htaccess file works to do the job.  I can point to a cgi script to run when a particular file type is accessed.  Unfortunately, I only want to run the script if the browser points directly to the image (ie www.mysite.com/image.jpg).  Right now, the script is trying to run for images embedded within my webpages.  Is there a way to have Apache treat the two cases different?

Ryan
Commented:
Ryan,

There is a technique to prevent image-theft (http://httpd.apache.org/docs/misc/FAQ.html#image-theft), where most of the time, people would direct-link the image from other website (or typing the image URL in the location bar).

http://httpd.apache.org/docs/misc/FAQ.html#image-theft

You could combine the Files/FilesMatch directive

http://httpd.apache.org/docs/mod/core.html#files
http://httpd.apache.org/docs/mod/core.html#filesmatch

OR

You could utilize mod_rewrite to rewrite the URL.
http://www.engelschall.com/pw/apache/rewriteguide/
http://www.engelschall.com/pw/apache/rewriteguide/#ToC38

Remember that if the <img> url is in the HTML docs, it should carry HTTP_REFERER header, and those url typed in the browser location bar would not have this header.  

The approach should work.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial