Example 1: New website on example.com
Input:
- Domain: example.com
- Enforce HTTPS and always use the address without www
- GZIP compression on
- Caching: images for 1 month, CSS and JavaScript for 1 week, always revalidate HTML
- Disable directory listings
Output (.htaccess):
# .htaccess generated with the ZAP-Hosting .htaccess Generator
# Before uploading: create a backup copy and test it in a staging environment first.
# HTTPS and domain format
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# General and Security
Options -Indexes
# Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript application/javascript application/json application/ld+json application/xml application/xhtml+xml application/rss+xml application/atom+xml image/svg+xml image/x-icon font/ttf font/otf application/vnd.ms-fontobject
</IfModule>
# Browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"
ExpiresByType image/avif "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/html "access plus 0 seconds"
ExpiresByType application/xhtml+xml "access plus 0 seconds"
</IfModule>
In words: An address such as http://www.example.com/seite is redirected with a single 301 to https://example.com/seite. Requests using http:// without www are also redirected to https:// with a 301. Images stay in the browser cache for 1 month (max-age=2592000), CSS and JavaScript for 1 week, while HTML pages are checked again on every visit. HTML, CSS, JavaScript, and JSON are transferred with GZIP compression. Anyone opening a directory without an index file now sees a 403 error instead of a file listing.
