← Tutti gli strumenti di sicurezza di pwn.watch
Generatore .htaccess: reindirizzamenti, HTTPS e header di sicurezza
Crea un file .htaccess Apache: forza HTTPS, scegli www o non-www, aggiungi reindirizzamenti 301, imposta gli header HSTS / CSP / X-Frame-Options, blocca hotlink e IP, e attiva cache e gzip.
Quick security presets
HTTPS e host canonico
Reindirizzamenti 301
→
Il vecchio percorso deve iniziare con /. Destinazione: un percorso o un URL https:// completo.
Header di sicurezza
Ti serve una CSP più restrittiva? Creane una con il generatore di CSP.
Controllo degli accessi e protezione
Prestazioni
.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>
Sponsorizzato
Offerto daTinkerer Club
Il club privato per chi possiede i propri server, usa la propria AI e paga 0 $ al mese per il software.
FAQ su .htaccess
- Come forzo HTTPS con .htaccess?
- Attiva mod_rewrite e aggiungi: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Se sei dietro un proxy o un bilanciatore di carico, controlla %{HTTP:X-Forwarded-Proto} invece di %{HTTPS}.
- Come creo un reindirizzamento 301 in .htaccess?
- Per un singolo percorso usa mod_alias: Redirect 301 /old-page /new-page. Per spostare un intero dominio usa mod_rewrite con RewriteCond %{HTTP_HOST} sul vecchio host e RewriteRule ^(.*)$ https://new.example/$1 [L,R=301].
- Quali header di sicurezza dovrei impostare in .htaccess?
- Strict-Transport-Security (HSTS), Content-Security-Policy, X-Frame-Options o CSP frame-ancestors, X-Content-Type-Options: nosniff, Referrer-Policy e Permissions-Policy. Serve mod_headers. Attiva HSTS solo quando tutti i sottodomini usano HTTPS.
- Come disattivo l'elenco delle directory?
- Aggiungi Options -Indexes. Apache restituirà 403 per le cartelle senza un file index, invece di mostrarne il contenuto.
- Perché il mio file .htaccess non ha effetto?
- Il vhost deve consentire gli override (AllowOverride All, o almeno FileInfo, Options, AuthConfig, Limit) e i moduli usati (mod_rewrite, mod_headers, mod_expires, mod_deflate) devono essere caricati. Nginx ignora completamente .htaccess.
- Viene inviato qualcosa a un server?
- No. Il file viene creato nel tuo browser; niente di ciò che inserisci viene caricato.