3

I'm using laravel and in that public directory I'm using this .htaccess to redirect every jpg, jpeg or png file to php file whch can generate webp file. Problem is everything is working on localhost and but in shared hosting .htaccess not working.

public/.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Redirect images to webp-on-demand.php (if browser supports webp)
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)\.(jpe?g|png)$ webpondemand.php?source=%{SCRIPT_FILENAME} [NC,L]

    # https://help.heroku.com/J2R1S4T8/can-heroku-force-an-application-to-use-ssl-tls
    # https://stackoverflow.com/a/34065445

    # rewrite to HTTPS start
    # If we receive a forwarded http request from a proxy...
    #RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

    # ...or just a plain old http request directly from the client
    #RewriteCond %{HTTP:X-Forwarded-Proto} =""
    #RewriteCond %{HTTPS} !=on

    # Redirect to https version
    #RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # rewrite to HTTPS end

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
<FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

AddType image/webp .webp

#php_flag expose_php Off
#php_value expose_php Off
php_value memory_limit 512M
php_value post_max_size 512M
php_value upload_max_filesize 512M

public/webpondemand.php

<?php
// To start with, lets display any errors.
// - this will reveal if you entered wrong paths
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Once you got it working, make sure that PHP warnings are not send to the output
// - this will corrupt the image
// For example, you can do it by commenting out the lines below:
// error_reporting(0);
// ini_set("display_errors", 0);

require '../vendor/autoload.php';        // Make sure to point this correctly

use WebPConvert\WebPConvert;

$source = $_GET['source'];            // Absolute file path to source file. Comes from the .htaccess
$destination = $source . '.webp';     // Store the converted images besides the original images (other options are available!)

$options = [

    // UNCOMMENT NEXT LINE, WHEN YOU ARE UP AND RUNNING!
    'show-report' => true             // Show a conversion report instead of serving the converted image.

    // More options available!
    // https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/converting/introduction-for-converting.md
    // https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/serving/introduction-for-serving.md
];
WebPConvert::serveConverted($source, $destination, $options);

I'm using rosell-dk/webp-convert to convert images to webp on the fly. It's necessary to do on the fly because of client requirement.

Example image url: https://www.example.com/storage/vacations/March2020/EBK2yfKf6pph0uCK90pO.jpg

It's working on localhost but not shared hosting. If I access directly webpondemand.php and pass source parameter storage/vacations/March2020/EBK2yfKf6pph0uCK90pO.jpg then it'll convert but If I access directly then it doesn't. Obviously it is .htaccess error because if it redirect to webpondemand.php then this file will convert it to webp as it did in localhost. But it's not redirecting to webpondemand.php file.

  • The link you shared is redirecting to webp image if it exist. That's not what I want. I want to redirect to *webpondemand.php* file if it's jpg, jpeg or png. so It can convert and save webp file on server and on next request deliver that webp image from this file – Faizan Anwer Ali Rupani Mar 28 '20 at 15:05
  • Sorry my bad.! The original file name is `webp-on-demand.php` and htaccess setup is `RewriteRule ^(.*)\.(jpe?g|png)$ webp-on-demand.php?source=%{SCRIPT_FILENAME} [NC,L]` you changed file name to `webpondemand.php` Make sure if htaccess finds your new file name. –  Mar 28 '20 at 15:25
  • check my .htaccess I also changed that. I'm not that stupid – Faizan Anwer Ali Rupani Mar 28 '20 at 15:28

1 Answers1

0

Turns out if was image cache problem by dns service provider aka cloudfare. All I did was Go to domain -> Caching tab and Purge everything. Now every new request for images are redirected to my php file which convert to webp and store in same directory and send png and jpeg images as image/webp type without changing image url or path or adding .webp extension