Skip to content

Select converted images via .htaccess#94

Merged
Sreini merged 52 commits intotinify:masterfrom
wcreateweb:task/conversion-server-side
Mar 12, 2026
Merged

Select converted images via .htaccess#94
Sreini merged 52 commits intotinify:masterfrom
wcreateweb:task/conversion-server-side

Conversation

@tijmenbruggeman
Copy link
Collaborator

This PR will extend our conversion feature with the delivery of converted images through htaccess.

When selecting and saving the delivery "htaccess", it will write the following block into the htaccess on the home path and the uploads directory.

# BEGIN tiny-compress-images
# The directives (lines) between "BEGIN tiny-compress-images" and "END tiny-compress-images" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteOptions Inherit
RewriteCond %{HTTP_ACCEPT} image/avif
RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png|gif)$
RewriteCond %{DOCUMENT_ROOT}/%1.avif -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(?:jpe?g|png|gif)$ $1.avif [T=image/avif,L]

RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png|gif)$
RewriteCond %{DOCUMENT_ROOT}/%1.webp -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(?:jpe?g|png|gif)$ $1.webp [T=image/webp,L]
</IfModule>

<IfModule mod_headers.c>
<FilesMatch "\.(jpe?g|png|gif)$">
Header append Vary Accept
</FilesMatch>
</IfModule>

<IfModule mod_mime.c>
AddType image/webp .webp
AddType image/avif .avif
</IfModule>
# END tiny-compress-images
  1. This is inserted using insert_with_markers when the setting for conversion delivery is set to server rules.
  2. There is a uninstall.php file which will delete the rule when the plug-in is uninstalled.
  3. The htaccess is stored in wp-content/uploads as we only focus on the uploads directory.

Settings
There is a new setting to allow the optimized image delivery via .htaccess. This setting is only visible when the user is on apache and has mod_rewrite.
conversion_settings

Follow-up

  • When the plug-in is installed after a previous installation and the delivery method setting is still on htaccess, it will not install the rules. Because it requires quite a bit of refactoring to reset the settings as well, this will be done in a seperate PR.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants