Web Server Optimization

Server Rules Generator (Nginx & Apache)

Direct static delivery for AVIF, WebP, SVG, and ICO assets with 0 PHP execution. Copy-paste these production-ready configs into Nginx or .htaccess.

Server Software
Uploads Path
Next-Gen Formats
Browser Cache Duration
Security & Cache Headers
optiforge-rules.conf
# ============================================================
#  OptiForge — Production Nginx Rules
#  Dual Accept Header Content-Negotiation & Direct File Delivery
# ============================================================

# 1. Map client Accept header for dual-format negotiation (Add inside http block)
map $http_accept $ofio_suffix {
    "~*image/avif" ".avif";
    "~*image/webp" ".webp";
    default "";
}

# 2. Direct static serving for next-gen sidecars, SVGs, and web fonts
location ~* ^/wp-content/uploads/.*\.(avif|webp|svg|ico|bmp|woff2)$ {
    try_files $uri /index.php?$args;
    add_header X-Content-Type-Options "nosniff";
    add_header Access-Control-Allow-Origin "*";
    add_header Vary Accept;
    expires 31536000s;
    add_header Cache-Control "public, immutable";
}

# 3. Content-negotiated delivery for legacy JPEG and PNG uploads
location ~* ^/wp-content/uploads/(.*)\.(jpe?g|png)$ {
    add_header Vary Accept;
    add_header X-Content-Type-Options "nosniff";
    expires 31536000s;
    add_header Cache-Control "public, immutable";
    try_files /wp-content/uploads/$1.$2$ofio_suffix $uri /index.php?$args;
}

Production Nginx Block (with $http_accept Map)

Place the map in /etc/nginx/nginx.conf (inside http {}) and the location blocks inside your server {} configuration.

# In /etc/nginx/nginx.conf (inside http {} block):
map $http_accept $ofio_avif_suffix {
    default "";
    "~*image/avif" ".avif";
}

map $http_accept $ofio_webp_suffix {
    default "";
    "~*image/webp" ".webp";
}

# In your virtual host server {} block:
location ~* ^/wp-content/uploads/(.+)\.(png|jpe?g)$ {
    add_header Vary Accept;
    add_header X-Content-Type-Options "nosniff";
    expires 365d;
    add_header Cache-Control "public, immutable";
    try_files $uri$ofio_avif_suffix $uri$ofio_webp_suffix $uri =404;
}

# Static assets pass-through with immutable caching:
location ~* ^/wp-content/uploads/.*\.(avif|webp|svg|ico|bmp|woff2)$ {
    add_header Access-Control-Allow-Origin "*";
    add_header Cache-Control "public, immutable";
    expires 365d;
    try_files $uri =404;
}

Production Apache / LiteSpeed .htaccess Rules

Append this block to the very top of your WordPress root .htaccess file before # BEGIN WordPress.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 1. Serve AVIF sidecar if supported and file exists
RewriteCond %{HTTP_ACCEPT} image/avif
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME}.avif -f
RewriteRule ^(wp-content/uploads/.+)\.(jpe?g|png)$ $1.$2.avif [T=image/avif,E=accept:1,L]

# 2. Serve WebP sidecar if supported and file exists
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule ^(wp-content/uploads/.+)\.(jpe?g|png)$ $1.$2.webp [T=image/webp,E=accept:1,L]
</IfModule>

<IfModule mod_headers.c>
    # Ensure browsers and CDNs vary cache by Accept header
    Header append Vary Accept env=REDIRECT_accept
    <FilesMatch "\.(avif|webp|svg|ico|woff2)$">
        Header set Cache-Control "max-age=31536000, public, immutable"
        Header set X-Content-Type-Options "nosniff"
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>