Documentazione

Server Rewrite (.htaccess)

Use Apache .htaccess rules to serve WebP and AVIF images at the server level — faster than PHP rewriting and compatible with all caching plugins.

Server Rewrite (.htaccess)

Server Rewrite adds rewrite rules to your .htaccess file so Apache (or LiteSpeed) serves WebP/AVIF directly — before PHP executes. This is faster than PHP-based delivery and eliminates the need for WordPress to process image URLs on every page load.

Nginx users: .htaccess rules don't apply to Nginx. Use PHP delivery mode or configure Nginx rewrite rules manually.


Table of Contents#


When to Use Server Rewrite#

SituationRecommendation
Apache/LiteSpeed hosting✅ Use Server Rewrite
Nginx hosting❌ Use PHP delivery
Shared hosting (cPanel/Plesk)✅ Usually works
WP-CLI / automation environments✅ Supported
CDN in front of your server⚠️ Test carefully — CDN may cache the wrong format

Enable Server Rewrite#

  1. Go to GetWebP → Settings → Delivery.
  2. Click Server Rewrite tab.
  3. Review the rules preview.
  4. Click Write .htaccess Rules.

GetWebP writes the rules inside a clearly marked block:

# BEGIN GetWebP
... rules ...
# END GetWebP

This block is preserved across WordPress updates and WP-CLI operations.


What Gets Added to .htaccess#

# BEGIN GetWebP
<IfModule mod_rewrite.c>
  RewriteEngine On
 
  # AVIF: serve .avif sibling when browser accepts image/avif
  RewriteCond %{HTTP_ACCEPT} image/avif
  RewriteCond %{REQUEST_FILENAME} \.(jpe?g|png|gif)$
  RewriteCond %{REQUEST_FILENAME}\.avif -f
  RewriteRule ^ %{REQUEST_URI}.avif [T=image/avif,L]
 
  # WebP: serve .webp sibling when browser accepts image/webp
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{REQUEST_FILENAME} \.(jpe?g|png|gif)$
  RewriteCond %{REQUEST_FILENAME}\.webp -f
  RewriteRule ^ %{REQUEST_URI}.webp [T=image/webp,L]
</IfModule>
 
<IfModule mod_headers.c>
  Header append Vary Accept env=REDIRECT_accept
</IfModule>
# END GetWebP

The Vary: Accept header tells CDNs and proxy caches to store separate versions for clients that support WebP/AVIF and those that don't.


Verify It's Working#

  1. Open your browser DevTools → Network tab.
  2. Load a page with images.
  3. Click an image request and check the Response Headers.
  4. You should see Content-Type: image/webp (or image/avif).

Alternatively, run:

curl -I -H "Accept: image/webp" https://yoursite.com/wp-content/uploads/photo.jpg

The response Content-Type should be image/webp.


Disable Server Rewrite#

  1. Go to GetWebP → Settings → Delivery → Server Rewrite.
  2. Click Remove .htaccess Rules.

The # BEGIN GetWebP ... # END GetWebP block is removed from .htaccess. PHP delivery continues to function if it was enabled.


Troubleshooting#

Images are still served as JPEG/PNG after enabling Server Rewrite

  • Confirm mod_rewrite is enabled on your server.
  • Check that the .htaccess file in your WordPress root is writable.
  • Make sure the .webp sibling files exist — run a batch conversion first.

Getting a 404 or broken image

  • The rewrite rule fires before checking if the file exists. Verify that %{REQUEST_FILENAME}\.webp -f condition is matching correctly.
  • On some shared hosts, RewriteBase may be required. Add RewriteBase / inside the <IfModule mod_rewrite.c> block.

CDN serving the wrong format

  • Ensure your CDN respects the Vary: Accept header and stores format-specific cache entries.
  • CloudFlare: Enable Polish with WebP support, or whitelist Vary: Accept in your page rules.