What I want to do is redirect all images (jpeg, png, gifs?) to a index.php?q=file and that handler will use a <img src=file>
tag.
But I'm afraid that when calling the file from that tag it will create an infinite loop.
So basically what I'm asking is how to redirect only outside calls to an image.
(Or is there a better way to protect my images?)

- 45,515
- 15
- 108
- 150

- 53
- 8
-
Can you show your code? – joel goldstick Jan 27 '16 at 14:45
2 Answers
That rule creates a handler which takes all matching requests to imageHandler.php
Please can you try the rule below;
RewriteRule ^(.*)(.jpg|.jpeg|.png|.gif|.bmp|.etc)$ imageHandler.php?f=%{REQUEST_FILENAME} [QSA,L]
And you will get $_GET["f"]
as the file name which requested and the $_GET["f"]
represents the path of the file on server.
PS: This rule wont cause the infinite loop for your requests just imageHandler.php will execute some processes and you can decide return the file or not. Please mind that you have to implement a binary output for imageHandler.php which can cause cost for your operations. To reduce cost of that operation you may have to implement a client-cache or server-cache mechanism for imageHandler.php.
That rule creates a redirect handler which redirects all matching requests to index.php
RewriteCond %{HTTP_REFERER} ^$ #That means referer is empty which means direct access to file
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com.* #That means referer is not my domain because when you use a file in <img> tag that will create a request to that image with referer of embedding address
RewriteCond %{REQUEST_URI} !^index.php #That means request not targeted to index.php (which a part of endless loop)
RewriteCond %{REQUEST_FILENAME} -f #That means request targeted to a file
RewriteCond %{REQUEST_URI} (.jpg|.jpeg|.png|.gif|.bmp)$ #That means request ends with .ext
RewriteRule ^(.*)(.jpg|.jpeg|.png|.gif|.bmp|.etc)$ http://example.com/index.php?f=$1 [R=302,L,QSA] #That means redirect request to index.php?f=file
I do not have a chance to test second one due I do not have a linux environment but production and its not possible to test that in production environment.
Hope these information helps you about your situation.

- 2,128
- 1
- 16
- 26
-
if there is
, wont the client ask the server for jjj.jpg and it will take them to the index again who will do
again and so on? or you meant to open the jpg from the handler so no call to jpg again? – user3720583 Jan 27 '16 at 15:33
-
@user3720583 I can not get what you mean ? In my answer each request for "jjj.jpg" will fall into imageHandler.php and you will get file full name with "f" for example: /etc/hosting/www.mydomain/public_html/images/jjj.jpg – Cihan Uygun Jan 27 '16 at 15:50
-
I'm sorry for my lousy explanation :) What I mean is, suppose someone tries to reach www.mydomain.com/img.jpg, I want him to be redirected to www.mydomain.com/index.php?file=img.jpg . in my index.php, all I Have is some text, and an img tag
(from the index.php) and when a browser gets an img tag it opens a new socket/request to the server with img.jpg, and if it sends them to index again, then it will be an infinite loop. – user3720583 Jan 27 '16 at 18:16
-
@user3720583 oh thats more clear now, so you should use Starkeen's solution for your question. But you need add more control to your .htaccess like checking referrer and else because as you mentioned that will cause an endless loop. I'm going to add a proper answer for you. – Cihan Uygun Jan 27 '16 at 18:24
You can use this :
RedirectMatch ^/(.+\.(jpg|png|gif))$ /index.php?f=$1
This will redirect any request that ends with .jpg , png, or gif to /index.php.
Note that, this is an external redirection, You can use the rewriteRule provided by @cihanUygun ,if you want to internally redirect these requests to index.php.

- 40,709
- 21
- 93
- 115
-
whats the diff between internal and external? what makes rewriteRule internal and RedirectMatch external? – user3720583 Jan 27 '16 at 15:35
-
External redirection changes the url in browser, while the internal redirection doesnt. Forex : if you go to /foo.png RedirectMatch will redirect you to a new location /index.php, changing the address bar from foo.png to /index.php?f=foo.png. – Amit Verma Jan 27 '16 at 15:42