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:
.htaccessrules don't apply to Nginx. Use PHP delivery mode or configure Nginx rewrite rules manually.
Table of Contents#
- When to Use Server Rewrite
- Enable Server Rewrite
- What Gets Added to .htaccess
- Verify It's Working
- Disable Server Rewrite
- Troubleshooting
When to Use Server Rewrite#
| Situation | Recommendation |
|---|---|
| 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#
- Go to GetWebP → Settings → Delivery.
- Click Server Rewrite tab.
- Review the rules preview.
- 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 GetWebPThe 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#
- Open your browser DevTools → Network tab.
- Load a page with images.
- Click an image request and check the Response Headers.
- You should see
Content-Type: image/webp(orimage/avif).
Alternatively, run:
curl -I -H "Accept: image/webp" https://yoursite.com/wp-content/uploads/photo.jpgThe response Content-Type should be image/webp.
Disable Server Rewrite#
- Go to GetWebP → Settings → Delivery → Server Rewrite.
- 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_rewriteis enabled on your server. - Check that the
.htaccessfile in your WordPress root is writable. - Make sure the
.webpsibling 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 -fcondition is matching correctly. - On some shared hosts,
RewriteBasemay be required. AddRewriteBase /inside the<IfModule mod_rewrite.c>block.
CDN serving the wrong format
- Ensure your CDN respects the
Vary: Acceptheader and stores format-specific cache entries. - CloudFlare: Enable Polish with WebP support, or whitelist
Vary: Acceptin your page rules.