webp generator via .htaccess - php

I am working with WordPress and I would like to have support for webp, but without installing an additional plugin. I thought it could be solved with a few .htaccess rules and a PHP script, but I don't have enough experience to create such a rule. Because different plugins and different themes may have different files, I thought that the wp-content/uploads/ directory would be a good place for webp files: wp-content/uploads/webp/.
I want the rule to detect at the start whether the request has webp support:
RewriteCond %{HTTP_ACCEPT} image/webp
The next condition that must be met is that the request must only be for files with the jpg, png extension, excluding webp.
If a file with the .webp suffix is found in the wp-content/uploads/webp/ directory -> return the webp file.
If the file does not exist, it should go to the webp.php file - there I have a script that generates the appropriate webp file on the fly and saves it in the appropriate place.
For example:
Request to https://example.com/wp-content/uploads/test.webp will return the webp file - the rule was not fulfilled because the file extension is webp. If the file does not exist, WordPress will return 404.
Request to https://example.com/wp-content/uploads/test.jpg - the rule checks whether a file exists in the wp-content/uploads/webp/wp-content/uploads/test.jpg.webp directory -> if it exists, it will return the webp file, otherwise the webp.php script will run.
Example code, but it was my trying:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond app/uploads/webp/$1.webp -f
RewriteRule (.*).(png|jpg)$ app/uploads/webp/$1.$2.webp [NC,L]
RewriteRule (.*).(png|jpg)$ webp.php [NC,L]
</IfModule>

Related

How to redirect all PNG, JPG and JPEG links to a specific PHP file via GET request

I am developing a WordPress plugin that optimizes the website. Currently, I am trying to convert all the images (PNG, JPG and JPEG) images to Webp using Rosell-dk's webp library https://github.com/rosell-dk/webp-on-demand which supports sending the image links via .htaccess as a GET request. But unfortunately it doesn't work, I am not sure if I am doing it wrong or if my approach is completely wrong.
I tried writing the rules that were stated by Rosell-dk on the GitHub page https://github.com/rosell-dk/webp-on-demand but unfortunately they doesn't work. The following is the rules that I have pasted on the .htaccess file which I created in my WordPress installation's 'wp-content' folder as all the images for a WordPress website resides there.
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect images to webp-on-demand.php (if browser supports webp)
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteRule ^(.*)\.(jpe?g|png)$ /plugins/*my plugin name*/inc/webp-converter/webp-on-demand.php?source=%{SCRIPT_FILENAME} [NC,L]
</IfModule>
And in my 'webp-on-demand.php' I have the following
$imageSource = $_GET['source'];
Unfortunately the file 'webp-on-demand.php' doesn't get called at all.
It would be great if y'all could help me figure out the correct rules or where I am wrong.
As you mentioned you are using nginx below steps should help you.
First, check whether mime type has webp
cat /etc/nginx/mime.types | grep webp
it should show you
image/webp webp;
now
server{
location ~* ^(/wp-content/.+)\.(png|jpe?g)$ {
set $base $1;
set $webp_uri $base$webp_suffix;
set $webp_old_uri $base.$2$webp_suffix;
set $root "<<FULL PATH OF wp-content PARENT>>";
root $root;
add_header Vary Accept;
if ( !-f $root$webp_uri ) {
rewrite ^(.*)\.(jpe?g|png)$ /wp-content/plugins/*your plugin name*/inc/webp-converter/webp-on-demand.php?source=$webp_uri break;
}
try_files $webp_uri $webp_old_uri $uri =404;
}
}
make sure you replace <<FULL PATH OF wp-content PARENT>> with the actual disk path

htaccess redirect files of type to php script and pass filename

I have a subdirectory on my site containing jpg images, e.g. site.com/files/image1.jpg
I would like to redirect these all to a php script that will handle the delivering of the files. I have tested various rewrite rules but can't seem to get it working right. I almost have the following working, but it seems to only work when the file does NOT exist, e.g. if I enter site.com/files/asdasdasd.jpg it will work, because that file doesn't exist, but image1.jpg exists and the image is immediately delivered for some reason.
P.S. is it best practice to have the .htaccess file and the PHP script in the root directory or in the same subdirectory i.e. 'files'
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^files/(.*)$ deliver.php?file=$1 [L]
</IfModule>

Is it possible to use PHP script for processing a non-PHP file by calling the file URL directly?

I need to make a PHP script that will take a filename as an argument, open that file and process it (read and process non-PHP pseudo instructions) and return the result to visitor's browser.
Example:
Web site visitor types http://domain.ext/somefilename.myext and web server opens specific PHP script (e.g. MyMainScript.php) passing full path to somefilename.myext as an argument.
MyMainScript.php opens the file somefilename.myext, reads whatever is in it, processes it and sends output back to visitor's browser
So my question is how to configure web server (IIS or Apache) to map files with file extension "myext" (*.myext) to execute script " MyMainScript.php"
Calling it using some other way, like:
/MyMainScript.php/somefilename.myext
or
/MyMainScript.php?file=somefilename.myext
is not an option
Create a .htaccess file at the root of your website. If you are running Apache2 # Ubuntu/Debian, use this directive:
AddType application/x-httpd-php .myext
If your are running PHP as CGI, you should write this instead:
AddHandler application/x-httpd-php .myext
This will treat specific extensions as PHP files and execute them as PHP.
Another solution might be a RewriteRule, so you can specify which file needs to be executed, for Apache your .htaccess file should look like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^somefilename.myext$ ./MyMainScript.php?file=somefilename.myext
# or just for any file ending with .myext
RewriteRule ^(.*).myext$ ./MyMainScript.php?file=$1.myext
Something like this should work.

Rewrite PNG URL to PHP File

Please forgive me, but I'm VERY new to PHP, and even worse with url rewrites.
I'd like to write a PHP script that will dynamically output an image that will then be used as a signature on a forum that I belong to.
I have a base PHP file that I'm working with and will be editing that so I'm able to host it for other users of this forum.
The information will be stored in a database, and I'd like to call the PHP script with a PNG URL
Example:
http://somedomain.com/somecode.png
rewriting that to
http://somedomain.com/sig_img.php?img=somecode
Where somecode is the database table primary index.
I don't really need help with the PHP script (yet), but I have no clue where to begin with the .htaccess mod_rewrite code.
Thank you all for any assistance!
Assuming that mod_rewrite is enabled, something like this should work:
<Location />
RewriteEngine On
# ensure requested resource is not a file.
RewriteCond %{SCRIPT_FILENAME} !-f
# ensure requested resource is not a directory.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*\.png)$ sig_img.php?img=$1 [NC,L]
</Location>
This will redirect any request that is a not a file or directory to the script sig_img.php with the requested filename as the parameter. You may want to restrict this to only .PNG file requests, in which case please read about the options.
Make sure you treat the request as untrusted and parameterise the input; don't just concatenate $_GET['img'] into your query string.

RewriteRule based on file existence with another name

We currently use an image resizer that was written in PHP. The resizing script takes advantage of caching, and writes the generated resized images to a cache directory (aptly named cache/).
The process is such that a rewritten URL, for example:
domain.com/img/250x250/some-image.jpg
Will be re-written to:
domain.com/image.php?width=250&height=250&src=some-image.jpg&function=resizeCrop
Inside image.php, we make a check to see whether a file matches the resize in the cache/ dir. All cached images are stored with a name of cachFunctionWidth_height_originalName, so for the given example the generated image file inside cache/ would be named resizecrop250_250_some-image.jpg
At the moment, we're utilizing PHP's fpassthru() function to output the file to the browser if it exists. If it doesn't we use a combination of GD functions and algorithms to output the file to the browser.
My question is whether it will be possible for us to bypass the image.php completely if a resized image exists in the cache directory using HTACCESS (it's a Linux server). Essentially, we need to check for a bastardized name form before serving rewriting.
For example, here's some pseudo-code of what we'd like to achieve, but I don't think it's possible:
User requests file > domain.com/img/250x250/some-image.jpg
Check if file cache/resizecrop250_250_some-image.jpg exists
If it does, rewrite to cache/resizecrop250_250_some-image.jpg
If it doesn't, rewrite to domain.com/image.php?width=250&height=250&src=some-image.jpg&function=resizeCrop
If this is not possible through HTACCESS, any other suggestions would also be welcome.
I believe using mod_rewrite it is possible. Consider following rule:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# check if constructed image name exists in cache directory
RewriteCond %{DOCUMENT_ROOT}/cache/resizecrop$1_$2_$3 -f
# exists, redirect to cache/constructed-image-file-name
RewriteRule ^img/([0-9]+)x([0-9]+)/([^.]+\.(?:jpe?g|gif|bmp|png))$ /cache/resizecrop$1_$2_$3 [L,NC]
# doesn't exist then forward to image.php with required query parameters
RewriteRule ^img/([0-9]+)x([0-9]+)/([^.]+\.(?:jpe?g|gif|bmp|png))$ /image.php?width=$1&height=$2&src=$3&function=resizeCrop [L,NC]

Categories