← 所有 pwn.watch 安全工具
.htaccess 生成器:重定向、HTTPS 和安全响应头
生成 Apache .htaccess 文件:强制使用 HTTPS、选择是否使用 www、添加 301 重定向、设置 HSTS / CSP / X-Frame-Options 响应头、阻止盗链和指定 IP,并启用缓存和 gzip。
Quick security presets
HTTPS 和规范域名
301 重定向
→
旧路径必须以 / 开头。目标可以是路径或完整的 https:// URL。
安全响应头
需要更严格的 CSP?试试CSP 生成器。
访问控制与安全加固
性能
.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>
赞助内容
由以下赞助商呈现Tinkerer Club
为拥有自己的服务器、运行自己的 AI,并且每月软件开销为 0 美元的人打造的私人俱乐部。
.htaccess 常见问题
- 如何用 .htaccess 强制使用 HTTPS?
- 启用 mod_rewrite 并添加:RewriteEngine On、RewriteCond %{HTTPS} off、RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]。如果位于代理或负载均衡器后,请改用 %{HTTP:X-Forwarded-Proto} 检查,而不是 %{HTTPS}。
- 如何在 .htaccess 中设置 301 重定向?
- 重定向单个路径时使用 mod_alias:Redirect 301 /old-page /new-page。迁移整个域名时,使用 mod_rewrite,通过 RewriteCond %{HTTP_HOST} 匹配旧主机,并设置 RewriteRule ^(.*)$ https://new.example/$1 [L,R=301]。
- 应该在 .htaccess 中设置哪些安全标头?
- Strict-Transport-Security (HSTS)、Content-Security-Policy、X-Frame-Options 或 CSP frame-ancestors、X-Content-Type-Options: nosniff、Referrer-Policy 和 Permissions-Policy。需要启用 mod_headers。只有在所有子域名都支持 HTTPS 后,才启用 HSTS。
- 如何禁用目录列表?
- 添加 Options -Indexes。这样,Apache 会对没有索引文件的文件夹返回 403,而不是列出其中的内容。
- 为什么我的 .htaccess 文件不起作用?
- 虚拟主机必须允许覆盖设置(AllowOverride All,或至少允许 FileInfo、Options、AuthConfig、Limit),并且必须加载所用模块(mod_rewrite、mod_headers、mod_expires、mod_deflate)。Nginx 完全不读取 .htaccess。
- 会向服务器发送任何内容吗?
- 不会。文件会在浏览器中生成;你输入的内容不会上传。