.htaccess 產生器

產生 Apache .htaccess 重新導向、快取與安全規則,並檢查衝突

選項

標準網址
標準主機
結尾斜線

/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 只在 Apache 的 AllowOverride 設定允許這些指令時才會生效。

瀏覽器會移除檔名開頭的點,因此存成 htaccess.txt。請在伺服器上改名為 .htaccess。

發現項目

未發現衝突
錯誤:0 · 警告:0 · 重新導向:0

說明 (3)

  • 說明: 在終止 TLS 的負載平衡器或 CDN 後方,%{HTTPS} 為 off,HTTPS 規則可能形成迴圈。請先向主機商確認。

  • 說明: 需要 AllowOverride AuthConfig, FileInfo, Indexes, Options(或 All)。不在允許範圍內的指令會讓 Apache 回應 500。

  • 說明: 若未載入 mod_rewrite, mod_headers, mod_deflate, mod_filter, mod_expires,對應區塊會被靜默略過。

檔案內容

  • 伺服器選項
  • 檔案保護
  • 重寫規則
  • 回應標頭
  • 壓縮
  • 瀏覽器快取

錯誤:0 · 警告:0 · 重新導向:0

會產生什麼

產生器會為常見的重新導向、效能與安全需求撰寫 Apache 2.4 的 .htaccess 檔案:強制 HTTPS、統一標準主機、整理結尾斜線與 index.html、自訂 301/302/307/308 重新導向、gzip 壓縮、Expires 快取預設、目錄列表與點檔案保護、基本安全性標頭、選用的 HSTS 以及自訂錯誤頁。每個區段都附有註解,順序永遠固定,方便比對差異。

衝突檢查

每筆重新導向寫入前都會檢查:來源格式錯誤、目的地協定不安全或不符預期、重複規則、指向自己、A 到 B 再回到 A 的迴圈,以及多繞一次的導向鏈。會形成迴圈或永遠不會執行的規則不會寫入檔案,並列在「發現」中。標準主機與結尾斜線都是單選,因此無法同時啟用加上 www 與移除 www 這類互相矛盾的規則。

重要的 Apache 細節

所有重寫規則共用一個 IfModule mod_rewrite 區塊,只有一個 RewriteEngine On。HTTPS 與主機變更在一次重新導向內完成,結尾斜線規則會略過實際存在的檔案與目錄,斜線與首頁導向只作用於 GET 與 HEAD 要求。存取控制使用 Require all denied,絕不使用過時的 Order 與 Deny 語法。依賴模組的區塊都包在 IfModule 中,缺少模組時只會略過該區塊,不會讓網站出錯。

部署之前

.htaccess 只在伺服器的 AllowOverride 設定允許這些指令時生效,否則 Apache 會回應 500。請把檔案放在文件根目錄,因為自訂重新導向的路徑以它為基準。在終止 TLS 的代理或 CDN 後方,HTTPS 規則看不到原始協定,可能形成迴圈。請在 HTTPS 全面可用後再開啟 HSTS,並從較短的有效期開始。瀏覽器會把下載存成 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