0

According to :

.htaccess rewrite image file to php script

I am trying to Rewrite an image to an action, in cakephp 2.5. The .htaccess used is the one inside /app/webroot/ according to this post: Adaptive images CakePHP htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    RewriteRule ^img/logo.png$ /users/display_details [NC,L]
</IfModule>

The result displayed is 'missing controller' page, with.

<?php
class ImgController extends AppController {

}

Any guess to make this working ? Thanks.

zeflex
  • 1,487
  • 1
  • 14
  • 29
  • Does the image actually exist? If not, then all you need is to connect a CakePHP route with a matching path. – ndm Apr 11 '18 at 11:02
  • @ndm yes the image exists, if I don't set a rule in the .htaccess it display the image. I tried with the cakephp route way but it seems it's not meant to use it in this kind of case (rewrite image to action) – zeflex Apr 11 '18 at 12:02

2 Answers2

0

Your rule will not change any of the server variables that CakePHP looks up for matching its routes (PATH_INFO, REQUEST_URI, SCRIPT_NAME, HTTP_X_REWRITE_URL), so CakePHP will still see and match on /img/logo.png, it will ignore the REDIRECT_URL that is being set by your rule, hence it will look for ImgController.

You could try to set the PATH_INFO variable via the E (envelope) flag, like:

RewriteRule ^img/logo.png$ index.php [NC,L,E=PATH_INFO:/users/display_details]

This should set $_SERVER['PATH_INFO'] to /users/display_details, and CakePHP should be able to match a route against it.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110
0

I did not try your solution because I found a way to make it working.

Into the routes.php I added this line

Router::connect('/images/logo.png', array('controller' => 'users', 'action' => 'display_details'));

It's kind of weird, true but it works. The real image is /img/logo.png so inside the display_details , I have a code to display the real image and run some code into this function too.

zeflex
  • 1,487
  • 1
  • 14
  • 29