Adaptive images CakePHP htaccess

2019-02-07 16:48发布

问题:

I'm trying to get Matt Wilcox adaptive images to work with CakePHP without success. http://adaptive-images.com/

CakePHP comes with three .htaccess files of it's own. In in /, one in /app/ and one in /app/webroot/

I need to get these four files to work together in such a way that image-requests are sent to the file adaptive-images.php that I've put in the root.

This is the htaccess from adaptive-images

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Adaptive-Images -----------------------------------------------------------------------------------

    # Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
    # RewriteCond %{REQUEST_URI} !some-directory
    # RewriteCond %{REQUEST_URI} !another-directory

    RewriteCond %{REQUEST_URI} !assets

    # Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
    # to adaptive-images.php so we can select appropriately sized versions
    RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php

    # END Adaptive-Images -------------------------------------------------------------------------------
</IfModule>

This is cakes htaccess from root /

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

This is cakes htaccess from /app/

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

This is cakes htaccess from /app/webroot

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

I assume the rules from adaptive images has to come first in one of the files and potentially add an [L] for last rule or something. But the only thing I've managed to accomplish so far is breaking the paths to images /img/myimage.gifso that Cake starts to look for an ImgController instead.

The Apache logs doesn't give me anything of value.

Any suggestions?

Edit: Currently looks like this

My modified / - htaccess now looks like below. Cake gives me an error about a missing ImgController and no images are created in the ai-cache-folder that adaptive images is supposed to put them in.

<IfModule mod_rewrite.c>
   Options +FollowSymlinks
   RewriteEngine on

    RewriteCond %{REQUEST_URI} !assets
    RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php

   RewriteCond %{REQUEST_URI} !adaptive-images\.php [NC]
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

回答1:

Alright I got it working even if it's perhaps not the most optimal solution there is.

I only changed the htaccess in the /app/webroot-folder like below

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !debug_kit
    RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

and added the adaptive-images.php file to app/webroot It would perhaps be nicer to have the adaptive-images.php file in the Vendor directory but at least it works.

I made two changes in adaptive-images.php

$cache_path    = "app/tmp/cache/ai-cache";
$source_file    = $document_root.'/app/webroot'.$requested_uri;