?
Solved

Use htaccess to Redirect JPG Images to Custom PHP Page

Posted on 2009-02-10
7
Medium Priority
?
1,262 Views
Last Modified: 2013-11-10
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.
0
Comment
Question by:Rod_Goldenshaft
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 23604709
> get  the images to load in the php file,

i.e. you try to open them with fopen etc.?
0
 

Author Comment

by:Rod_Goldenshaft
ID: 23605361
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 23605573
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rod_Goldenshaft
ID: 23607472
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
 

Author Comment

by:Rod_Goldenshaft
ID: 23607477
oh and i will increase the points to 500.  i didn't realize that it was at 250.  
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 23607639
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
 

Author Comment

by:Rod_Goldenshaft
ID: 23612534
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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question