← เครื่องมือความปลอดภัยทั้งหมดของ pwn.watch
ตัวสร้าง .htaccess: รีไดเรกต์, HTTPS และ Security Header
สร้างไฟล์ Apache .htaccess: บังคับใช้ HTTPS, เลือก www หรือไม่มี www, เพิ่มรีไดเรกต์ 301, ตั้งค่า HSTS / CSP / X-Frame-Options header, บล็อก hotlink และ IP พร้อมเปิดใช้แคชและ gzip
Quick security presets
HTTPS และโฮสต์หลัก
รีไดเรกต์ 301
→
พาธเดิมต้องขึ้นต้นด้วย / ปลายทางใช้พาธหรือ URL https:// แบบเต็ม
ส่วนหัวความปลอดภัย
อยากได้ 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
- บังคับใช้ HTTPS ด้วย .htaccess ได้อย่างไร?
- เปิดใช้ mod_rewrite แล้วเพิ่ม: RewriteEngine On, RewriteCond %{HTTPS} off, RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] หากอยู่หลังพร็อกซีหรือโหลดบาลานเซอร์ ให้ทดสอบ %{HTTP:X-Forwarded-Proto} แทน %{HTTPS}
- เขียนการเปลี่ยนเส้นทาง 301 ใน .htaccess ได้อย่างไร?
- เปลี่ยนเส้นทางเฉพาะเส้นทางด้วย mod_alias: Redirect 301 /old-page /new-page หากต้องการย้ายทั้งโดเมน ให้ใช้ mod_rewrite กับ RewriteCond %{HTTP_HOST} เพื่อตรวจสอบโฮสต์เดิม และ RewriteRule ^(.*)$ https://new.example/$1 [L,R=301]
- ควรตั้งค่า security header ใดบ้างใน .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 เปิดใช้ HSTS เมื่อทุกโดเมนย่อยให้บริการ HTTPS แล้วเท่านั้น
- ปิดการแสดงรายการไฟล์ในไดเรกทอรีได้อย่างไร?
- เพิ่ม Options -Indexes จากนั้น Apache จะตอบกลับ 403 สำหรับโฟลเดอร์ที่ไม่มีไฟล์ index แทนการแสดงรายการไฟล์
- ทำไมไฟล์ .htaccess ถึงไม่มีผล?
- vhost ต้องอนุญาตให้ override (AllowOverride All หรืออย่างน้อย FileInfo, Options, AuthConfig, Limit) และต้องโหลดโมดูลที่ใช้ (mod_rewrite, mod_headers, mod_expires, mod_deflate) แล้ว Nginx จะไม่อ่าน .htaccess เลย
- มีการส่งข้อมูลไปยังเซิร์ฟเวอร์ไหม?
- ไม่มี ไฟล์จะถูกประกอบขึ้นในเบราว์เซอร์ของคุณ ไม่มีการอัปโหลดสิ่งที่คุณกรอก