ตัวสร้าง .htaccess

สร้างกฎเปลี่ยนเส้นทาง แคช และความปลอดภัยของ .htaccess สำหรับ Apache พร้อมตรวจข้อขัดแย้ง

ตัวเลือก

URL หลัก
โฮสต์หลัก
เครื่องหมายทับท้าย

/blog/index.html จะเปลี่ยนเส้นทางไป /blog/

การเปลี่ยนเส้นทางที่กำหนดเอง
การเปลี่ยนเส้นทาง: 0

ยังไม่มีการเปลี่ยนเส้นทางที่กำหนดเอง เพิ่มหนึ่งรายการหรือใช้การวางทีละหลายรายการ

.htaccess ที่สร้างขึ้น

# Generated by the CodingTool.dev .htaccess Generator for Apache 2.4.
# Needs AllowOverride AuthConfig FileInfo Indexes Options (or All); Apache answers 500 for directives it does not allow.
# Place this file in the document root. Test on a staging site before going live.

# Disable directory listings.
Options -Indexes

# Deny access to .htaccess, .htpasswd and other .ht files.
<FilesMatch "^\.ht">
  Require all denied
</FilesMatch>

# Deny backup, dump and editor swap files such as .bak, .old, .sql, .swp and ~ copies.
<FilesMatch "(\.(bak|old|sql|swp)|~)$">
  Require all denied
</FilesMatch>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # Block dotfiles and dot-folders such as .env and .git; keep /.well-known/ reachable.
  RewriteCond %{REQUEST_URI} !(^|/)\.well-known/
  RewriteRule (^|/)\. - [F]

  # Force HTTPS: redirect every HTTP request to the same host over HTTPS.
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

<IfModule mod_headers.c>
  # Basic security headers. Adjust Permissions-Policy if your site uses these features.
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
</IfModule>

# Compress text responses. AddOutputFilterByType needs mod_filter in Apache 2.4.
<IfModule mod_deflate.c>
  <IfModule mod_filter.c>
    # Text and markup
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    # Scripts and data
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/json application/ld+json application/manifest+json
    # Feeds and XML
    AddOutputFilterByType DEFLATE application/xml application/rss+xml application/atom+xml
    # SVG and uncompressed fonts
    AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/vnd.ms-fontobject
  </IfModule>
</IfModule>

# Browser caching (conservative): short lifetimes that are safe without versioned file names.
<IfModule mod_expires.c>
  ExpiresActive On
  # HTML and data: always revalidate
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"
  ExpiresByType application/ld+json "access plus 0 seconds"
  ExpiresByType application/xml "access plus 0 seconds"
  ExpiresByType text/xml "access plus 0 seconds"
  # Feeds
  ExpiresByType application/rss+xml "access plus 1 hour"
  ExpiresByType application/atom+xml "access plus 1 hour"
  # CSS and JavaScript
  ExpiresByType text/css "access plus 1 week"
  ExpiresByType text/javascript "access plus 1 week"
  ExpiresByType application/javascript "access plus 1 week"
  # Web app manifest
  ExpiresByType application/manifest+json "access plus 1 week"
  # Images
  ExpiresByType image/avif "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  # Fonts
  ExpiresByType font/woff "access plus 1 month"
  ExpiresByType font/woff2 "access plus 1 month"
  ExpiresByType font/ttf "access plus 1 month"
  ExpiresByType font/otf "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
  # Audio and video
  ExpiresByType audio/mpeg "access plus 1 month"
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"
</IfModule>

.htaccess ใช้ได้เฉพาะเมื่อการตั้งค่า AllowOverride ของ Apache อนุญาตคำสั่งเหล่านี้

บันทึกเป็น htaccess.txt เพราะเบราว์เซอร์ตัดจุดหน้าชื่อไฟล์ออก ให้เปลี่ยนชื่อเป็น .htaccess บนเซิร์ฟเวอร์

สิ่งที่พบ

ไม่พบข้อขัดแย้ง
ข้อผิดพลาด: 0 · คำเตือน: 0 · การเปลี่ยนเส้นทาง: 0

หมายเหตุ (3)

  • หมายเหตุ: หลัง load balancer หรือ CDN ที่จบ TLS ค่า %{HTTPS} จะเป็น off และกฎ HTTPS อาจวนซ้ำ โปรดสอบถามผู้ให้บริการโฮสต์ก่อน

  • หมายเหตุ: ต้องใช้ AllowOverride AuthConfig, FileInfo, Indexes, Options (หรือ All) คำสั่งที่อยู่นอกสิทธิ์จะทำให้ Apache ตอบกลับ 500

  • หมายเหตุ: บล็อกของ mod_rewrite, mod_headers, mod_deflate, mod_filter, mod_expires จะถูกข้ามอย่างเงียบ ๆ หากไม่ได้โหลดโมดูลนั้น

ในไฟล์นี้

  • ตัวเลือกเซิร์ฟเวอร์
  • การป้องกันไฟล์
  • กฎ rewrite
  • เฮดเดอร์การตอบกลับ
  • การบีบอัด
  • แคชเบราว์เซอร์

ข้อผิดพลาด: 0 · คำเตือน: 0 · การเปลี่ยนเส้นทาง: 0

สิ่งที่สร้างได้

ตัวสร้างนี้เขียนไฟล์ .htaccess สำหรับ Apache 2.4 ตามความต้องการทั่วไปด้านการเปลี่ยนเส้นทาง ประสิทธิภาพ และความปลอดภัย ได้แก่ บังคับ HTTPS ใช้โฮสต์หลักเดียว จัดการเครื่องหมายทับท้ายและ index.html การเปลี่ยนเส้นทาง 301/302/307/308 ที่กำหนดเอง การบีบอัด gzip ค่าแคช Expires สำเร็จรูป การป้องกันรายการโฟลเดอร์และไฟล์ที่ขึ้นต้นด้วยจุด เฮดเดอร์ความปลอดภัยพื้นฐาน HSTS แบบเลือกได้ และหน้าข้อผิดพลาดของคุณเอง ทุกส่วนมีคอมเมนต์ และลำดับคงที่เสมอ จึงเทียบความเปลี่ยนแปลงได้ง่าย

การตรวจข้อขัดแย้ง

ก่อนเขียนการเปลี่ยนเส้นทางแต่ละรายการ ระบบจะตรวจต้นทางที่รูปแบบผิด โปรโตคอลปลายทางที่ไม่ปลอดภัยหรือไม่คาดคิด รายการซ้ำ การเปลี่ยนเส้นทางไปที่ตัวเอง การวนจาก A ไป B แล้วกลับมา A และสายโซ่ที่เพิ่มการกระโดดหนึ่งครั้ง กฎที่จะวนซ้ำหรือไม่มีวันทำงานจะไม่ถูกใส่ในไฟล์และแสดงในผลการตรวจ โฮสต์หลักและเครื่องหมายทับท้ายเป็นตัวเลือกเดียว จึงเปิดกฎที่ขัดกันอย่างเติม www และตัด www พร้อมกันไม่ได้

รายละเอียด Apache ที่สำคัญ

กฎ rewrite ทั้งหมดอยู่ในบล็อก IfModule mod_rewrite เดียว และมี RewriteEngine On เพียงครั้งเดียว การเปลี่ยน HTTPS และโฮสต์ทำในการเปลี่ยนเส้นทางครั้งเดียว กฎเครื่องหมายทับท้ายข้ามไฟล์และโฟลเดอร์ที่มีอยู่จริง และการเปลี่ยนเส้นทางเรื่องทับหรือดัชนีใช้กับคำขอ GET และ HEAD เท่านั้น การควบคุมสิทธิ์ใช้ Require all denied ไม่ใช้ไวยากรณ์เก่า Order และ Deny บล็อกที่พึ่งโมดูลถูกครอบด้วย IfModule หากไม่มีโมดูลก็แค่ข้ามบล็อกนั้นโดยเว็บไซต์ไม่พัง

ก่อนนำไปใช้

.htaccess ใช้ได้เฉพาะเมื่อการตั้งค่า AllowOverride ของเซิร์ฟเวอร์อนุญาตคำสั่งเหล่านี้ มิฉะนั้น Apache จะตอบกลับ 500 วางไฟล์ไว้ที่ document root เพราะพาธของการเปลี่ยนเส้นทางที่กำหนดเองอ้างอิงจากตำแหน่งนั้น หลังพร็อกซีหรือ CDN ที่จบ TLS กฎ HTTPS จะมองไม่เห็นสคีมเดิมและอาจวนซ้ำ เปิด HSTS เมื่อ HTTPS ใช้ได้ทุกที่แล้วเท่านั้น และเริ่มจาก max-age สั้น ๆ เบราว์เซอร์จะบันทึกไฟล์ที่ดาวน์โหลดเป็น htaccess.txt ให้เปลี่ยนชื่อเป็น .htaccess บนเซิร์ฟเวอร์ ทุกอย่างทำงานในเบราว์เซอร์ของคุณ ไม่มีการอัปโหลดใด ๆ

CodingTool เปิดตัวอย่างเป็นทางการบน

Launched on StartupBaseCodingTool.dev - Free, Fast & Easy Developer Tools in One Place | Product HuntCodingTool - Featured on Startup FameFazier badgeListed on Turbo0Featured on Twelve ToolsFeatured on Findly.toolsLaunchpadly Startup DirectoryCodingTool.dev — Featured on Smol Launch