Use htaccess to Redirect JPG Images to Custom PHP Page

Hello, I've been all over this site for the past 2 days but I can't seem to find a solution to my problem.  I have a secured folder on my site that has directory view enabled.  The folder is called "line_sheets, and specific customers can log in to this folder to view advance images of our new products.  I have a php file called "images.php" with a header and some CSS on it and I want the jpg images to appear in that file.  

I tried several rewrites in the htaccess file, sending jpg requests to images.php.  But I cant get the images to load in the php file, as my php skills are lackluster.  I know this can be done, Im just not sure how.  Any advice would be greatly appreciated.  Thanks.
Rod_GoldenshaftAsked:
Who is Participating?
 
caterham_wwwConnect With a Mentor Commented:
Where did you place the rule? In your subfolder or in root? If it's in root, the path needs to be adjusted.
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^(line_sheets/[a-z0-9]+\.jpg)$ jpg.php?file=$1 [L,NC]
RewriteRule ^line_sheets/r\.([a-z0-9]+\.jpg)$ /line_sheets/$1 [L,NC]

Open in new window

0
 
caterham_wwwCommented:
> get  the images to load in the php file,

i.e. you try to open them with fopen etc.?
0
 
Rod_GoldenshaftAuthor Commented:
correct, i can't figure out how to get the php file to open the jpg image. i was using a rewrite that i found in another EE thread (http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_23298572.html) :

RewriteRule ^(.*\.jpg(/.*)?)$ jpg.php?file=$1 [L,QSA]

The rewrite rule works, now i need a way for the php script to reference the jpg image that the end user originally tried to open. hope that makes sense.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
caterham_wwwCommented:
If you've only specific characters, don't use such a wildcard but

RewriteRule ^([a-z0-9]+\.jpg)$ jpg.php?file=$1 [L]

a possible way could be a fake-reference, e.g. your php-script outputs +

<html>
<head>
<style type="text/css">
....
</style>
</head>
<body>
....
<img src="r.<?  echo $_GET['file'] ?>">
</body>
</html>

while r.(origname) causes the rule not to hit and we rewrite r.(origname) back to (origname) so that the referenced image in the <img> element could be served.

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^([a-z0-9]+\.jpg)$ jpg.php?file=$1 [L]
RewriteRule ^r.([a-z0-9]+\.jpg)$ $1 [L]

Open in new window

0
 
Rod_GoldenshaftAuthor Commented:
thanks for the help.  i made the changes that you suggested but unfortunately, the rewrite did not work.  the image just loads as normal in a blank window.  

when i changed the rewrite to what i was using previously - RewriteRule ^(.*\.jpg(/.*)?)$ jpg.php?file=$1 [L,QSA] - the php page loads but the image appears as missing (red X).  the url for the image (in properties) shows as http://mysite.com/line_sheets/r.image.jpg.    i tried removing the "r." from the php file, and then the image location was correct but still no image appearing.  any other suggestions?
0
 
Rod_GoldenshaftAuthor Commented:
oh and i will increase the points to 500.  i didn't realize that it was at 250.  
0
 
Rod_GoldenshaftAuthor Commented:
Awesome, that did it.  I think there was a slash missing in line 4.  Anyway, if it helps someone else down the line I am using this in my subfolder htaccess:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^([a-z0-9]+\.jpg)$ line_sheets.php?file=$1 [L,NC]
RewriteRule ^r\.([a-z0-9]+\.jpg)$ $1 [L,NC]

And then <img src="r.<? echo $_GET['file'] ?>" /> goes into the php file, in this case line_sheets.  

Thank you very much for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.