mowgli
mowgli

Reputation: 2869

htaccess: redirect images to a page that shows the images

I would like to redirect all images to a showimage.php, using htaccess

But if I try something like

RewriteRule ^images/([A-Za-z1-9]+).png$ showimage.php?image=$1 [L]

the image to be shown on that page will get redirected too (doh)

How to redirect all images, but not if they are requested from showimage.php? (or from this server)

Upvotes: 0

Views: 1702

Answers (1)

huelbois
huelbois

Reputation: 7012

There are two things you can do:

1- no redirect if the query comes from the server You just have to add a RewriteCond directive before the RewriteRule, which says that the following RewriteRule is not to be done if the source of the http query is this server.

RewriteCond %{REMOTE_ADDR} !127.0.0.1

Depending on how your script does the get, you may need to use instead your server's private IP. Or add another RewriteCond with this IP, to prevent both to be redirected.

2- you can also prevent redirect depending on the referer of the query, but you have to be sure that it is sent with the query. (referer is the source URL that requested your URL. For example, if you're on http://localhost/index.php and click a link to http://localhost/help.php, the referer of the last one will be http://localhost/index.php). You still use a RewriteCond, but with HTTP_REFERER instead of REMOTEHOST.

eg:

RewriteCond %{HTTP_REFERER} !showimage.php

RewriteCond %{HTTP_REFERER} !/images/.*\.png

You may have to toy a bit with the full referer, not user if this is enough, but just the idea to start with. You can check which referer you have in the access log (if you have %{Referer}i enabled in the LogFormat you use)

Got it : problem was that, the REFERER is the URL that was asked, and not the rewritten one (should I've thought about it, it's obvious). So, the problem is that when you require http://localhost/images/x.png, it is rewritten as http://localhost/showimage.php?image=x.png which serves the page with the inside. BUT the referer is NOT showimage.php but .../images/x.png (the initial URL). So, the RewriteCond on the HTTP_REFERER must check that the origin URL is something like /images/*.png instead.

Here is my working example. Rules are in [DOCROOT]/.htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !/images/.*\.png
RewriteRule ^images/([A-Za-z1-9-]+).png$ image.php?name=$1 [L]

Here is my image.php file:

<html>
Here it is : <img src="/images/<?php echo $_GET['name']; ?>.png"/>
</html>

With my browser, calling http://[server ip]/images/foo.png will display an html page, containing the words "Here it is : " AND the requested picture, taken from [DOCROOT]/images/foo.png.

For the "not from this server" to work, you just add the previous RewriteCond about REMOTE_ADDR before the RewriteRule (when you don't specify [OR], they work as AND conditions - the rewriterule is triggered only if both conditions are true - not from this server and not from the php script)

Upvotes: 2

Related Questions