We help IT Professionals succeed at work.

Use htaccess to Redirect JPG Images to Custom PHP Page

Rod_Goldenshaft
on
Medium Priority
1,867 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.
Comment
Watch Question

Top Expert 2008

Commented:
> get  the images to load in the php file,

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

Author

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.
Top Expert 2008

Commented:
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

Author

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?

Author

Commented:
oh and i will increase the points to 500.  i didn't realize that it was at 250.  
Top Expert 2008
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.