← All pwn.watch security tools
.htaccess Generator: Redirects, HTTPS & Security Headers
Build an Apache .htaccess file: force HTTPS, pick www or non-www, add 301 redirects, set HSTS / CSP / X-Frame-Options headers, block hotlinking and IPs, and turn on caching and gzip.
Quick security presets
HTTPS & canonical host
301 redirects
→
Old path must start with /. Target: a path or a full https:// URL.
Security headers
Need a stricter CSP? Build one with the CSP generator.
Access control & hardening
Performance
.htaccess
# Generated by pwn.watch/tools/htaccess-generator
# ----- Hardening -----
Options -Indexes
<FilesMatch "^\.(?!well-known)">
Require all denied
</FilesMatch>
<FilesMatch "\.(bak|config|sql|ini|log|sh|swp|dist)$">
Require all denied
</FilesMatch>
# ----- HTTPS, www & rewrite rules (mod_rewrite) -----
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
</IfModule>
# ----- 301 redirects (mod_alias) -----
Redirect 301 /old-page /new-page
# ----- Security headers (mod_headers) -----
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'self'; object-src 'none'; base-uri 'self'"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
Header unset X-Powered-By
</IfModule>
# ----- Brotli compression (mod_brotli) -----
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml text/javascript
AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json application/xml application/rss+xml image/svg+xml
</IfModule>
# ----- Gzip compression (mod_deflate) -----
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
AddOutputFilterByType DEFLATE application/javascript application/json application/xml application/rss+xml image/svg+xml
</IfModule>
# ----- Browser caching (mod_expires) -----
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 hour"
ExpiresByType text/html "access plus 0 seconds"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/avif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
</IfModule>
Sponsored
Brought to you byTinkerer Club
The private club for people who own their servers, run their own AI, and pay $0/month for software.
.htaccess FAQ
- How do I force HTTPS with .htaccess?
- Enable mod_rewrite and add: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Behind a proxy or load balancer, test %{HTTP:X-Forwarded-Proto} instead of %{HTTPS}.
- How do I write a 301 redirect in .htaccess?
- For one path use mod_alias: Redirect 301 /old-page /new-page. To move a whole domain use mod_rewrite with RewriteCond %{HTTP_HOST} on the old host and RewriteRule ^(.*)$ https://new.example/$1 [L,R=301].
- Which security headers should I set in .htaccess?
- Strict-Transport-Security (HSTS), Content-Security-Policy, X-Frame-Options or CSP frame-ancestors, X-Content-Type-Options: nosniff, Referrer-Policy and Permissions-Policy. They need mod_headers. Only enable HSTS once every subdomain serves HTTPS.
- How do I disable directory listing?
- Add Options -Indexes. Apache then returns 403 for folders without an index file instead of listing their contents.
- Why does my .htaccess file do nothing?
- The vhost must allow overrides (AllowOverride All, or at least FileInfo, Options, AuthConfig, Limit), and the modules used (mod_rewrite, mod_headers, mod_expires, mod_deflate) must be loaded. Nginx ignores .htaccess entirely.
- Is anything sent to a server?
- No. The file is assembled in your browser; nothing you enter is uploaded.