← Všechny bezpečnostní nástroje pwn.watch
Generátor .htaccess: přesměrování, HTTPS a bezpečnostní hlavičky
Vytvoř soubor .htaccess pro Apache: vynuť HTTPS, vyber www nebo bez www, přidej přesměrování 301, nastav hlavičky HSTS / CSP / X-Frame-Options, blokuj hotlinkování a IP adresy a zapni cachování a gzip.
Quick security presets
HTTPS a kanonický host
Přesměrování 301
→
Stará cesta musí začínat na /. Cíl: cesta nebo úplná https:// URL.
Bezpečnostní hlavičky
Potřebuješ přísnější CSP? Vytvoř si ho pomocí generátoru CSP.
Řízení přístupu a zabezpečení
Výkon
.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>
Sponzorováno
Přináší tiTinkerer Club
Soukromý klub pro lidi, kteří mají vlastní servery, provozují vlastní AI a za software platí 0 $ měsíčně.
FAQ k .htaccess
- Jak vynutit HTTPS pomocí .htaccess?
- Zapni mod_rewrite a přidej: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Za proxy nebo load balancerem otestuj %{HTTP:X-Forwarded-Proto} místo %{HTTPS}.
- Jak napsat přesměrování 301 v .htaccess?
- Pro jednu cestu použij mod_alias: Redirect 301 /old-page /new-page. Celou doménu přesuneš pomocí mod_rewrite s RewriteCond %{HTTP_HOST} na starém hostu a RewriteRule ^(.*)$ https://new.example/$1 [L,R=301].
- Které bezpečnostní hlavičky mám nastavit v .htaccess?
- Strict-Transport-Security (HSTS), Content-Security-Policy, X-Frame-Options nebo CSP frame-ancestors, X-Content-Type-Options: nosniff, Referrer-Policy a Permissions-Policy. Potřebují mod_headers. HSTS zapni až ve chvíli, kdy všechny subdomény používají HTTPS.
- Jak vypnout výpis obsahu adresářů?
- Přidej Options -Indexes. Apache pak pro složky bez indexového souboru vrátí 403 místo výpisu jejich obsahu.
- Proč můj soubor .htaccess nic nedělá?
- Vhost musí povolovat přepsání nastavení (AllowOverride All, nebo aspoň FileInfo, Options, AuthConfig, Limit) a použité moduly (mod_rewrite, mod_headers, mod_expires, mod_deflate) musí být načtené. Nginx soubory .htaccess úplně ignoruje.
- Odesílá se něco na server?
- Ne. Soubor se sestaví v prohlížeči; nic z toho, co zadáš, se nikam nenahrává.