← Tất cả công cụ bảo mật pwn.watch
Công cụ tạo .htaccess: chuyển hướng, HTTPS và header bảo mật
Tạo tệp .htaccess cho Apache: buộc dùng HTTPS, chọn www hoặc không www, thêm chuyển hướng 301, đặt header HSTS / CSP / X-Frame-Options, chặn hotlink và IP, bật bộ nhớ đệm và gzip.
Quick security presets
HTTPS và host chuẩn
Chuyển hướng 301
→
Đường dẫn cũ phải bắt đầu bằng /. Đích đến: đường dẫn hoặc URL https:// đầy đủ.
Header bảo mật
Cần CSP chặt chẽ hơn? Tạo bằng trình tạo CSP.
Kiểm soát truy cập & tăng cường bảo mật
Hiệu suất
.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>
Được tài trợ
Được tài trợ bởiTinkerer Club
Câu lạc bộ riêng dành cho những người sở hữu máy chủ, tự chạy AI và trả 0 đô/tháng cho phần mềm.
Câu hỏi thường gặp về .htaccess
- Làm sao buộc dùng HTTPS với .htaccess?
- Bật mod_rewrite rồi thêm: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]. Nếu dùng proxy hoặc bộ cân bằng tải, hãy kiểm tra %{HTTP:X-Forwarded-Proto} thay vì %{HTTPS}.
- Làm sao tạo chuyển hướng 301 trong .htaccess?
- Để chuyển hướng một đường dẫn, dùng mod_alias: Redirect 301 /old-page /new-page. Để chuyển cả domain, dùng mod_rewrite với RewriteCond %{HTTP_HOST} trên máy chủ cũ và RewriteRule ^(.*)$ https://new.example/$1 [L,R=301].
- Nên đặt những header bảo mật nào trong .htaccess?
- Strict-Transport-Security (HSTS), Content-Security-Policy, X-Frame-Options hoặc CSP frame-ancestors, X-Content-Type-Options: nosniff, Referrer-Policy và Permissions-Policy. Cần có mod_headers. Chỉ bật HSTS khi mọi subdomain đều dùng HTTPS.
- Làm sao tắt hiển thị danh sách thư mục?
- Thêm Options -Indexes. Apache sẽ trả về 403 cho thư mục không có file index thay vì liệt kê nội dung.
- Sao file .htaccess của mình không có tác dụng?
- Vhost phải cho phép ghi đè (AllowOverride All, hoặc ít nhất FileInfo, Options, AuthConfig, Limit), đồng thời phải tải các module đang dùng (mod_rewrite, mod_headers, mod_expires, mod_deflate). Nginx hoàn toàn bỏ qua .htaccess.
- Có dữ liệu nào được gửi lên máy chủ không?
- Không. File được tạo ngay trong trình duyệt; nội dung bạn nhập không được tải lên.