.htaccess 到 Nginx 的转换工具
免费的在线 htaccess 到 Nginx 转换器,无需安装。可将 Apache 的重写规则转换为 Nginx 配置文件
關於 .htaccess 轉 nginx 轉換器
解析 Apache .htaccess 指令並產生大致等價的 nginx 配置。它能夠處理 RewriteEngine、RewriteBase、RewriteCond、RewriteRule、RedirectMatch、ErrorDocument、Header set 以及 Options +Indexes/-Indexes。<IfModule> 區塊內部的指令會被處理;其他內容則被忽略。
如何使用
1. 將你的 .htaccess 內容貼到輸入文字框 2. 點擊 Convert 以解析並翻譯可識別的指令 3. 在輸出文字框中檢視產生的 nginx 配置 4. 點擊複製圖示複製結果 5. 若找不到可識別的指令,輸出區會顯示無可識別指令的提示訊息
支援的指令對應
RewriteRule 會轉換為 rewrite 或 location/return 區塊(R=301 對應 301,其餘情況為 302)。針對 %{HTTP_HOST}、%{REQUEST_FILENAME} 與 %{HTTPS} 的 RewriteCond 會轉換為 nginx 的 if (...) 區塊;其他條件則以註解形式輸出。RedirectMatch 轉換為 location ~ + return。ErrorDocument 轉換為 error_page。Header set 轉換為 add_header。Options -Indexes 轉換為 autoindex off。複雜條件、SetEnv、AuthType 以及大多數其他指令不會被翻譯。
▶產生的 nginx 配置可以直接用於正式環境嗎?
不行。它只是一個起點。Apache 與 nginx 的配置模型差異很大,因此輸出結果需要人工檢視。部署前請務必使用 nginx -t 進行測試。
▶為什麼部分 RewriteCond 行顯示為註解?
只有針對 %{HTTP_HOST}、%{REQUEST_FILENAME} 與 %{HTTPS} 的條件會被翻譯為 nginx 的 if 區塊。其他變數(QUERY_STRING、HTTP_USER_AGENT 等)都以註解形式輸出,需要人工處理。
▶它能處理 AuthType、Require 或 Limit 嗎?
不能。認證與存取控制指令不會被轉換。nginx 使用 auth_basic 等指令,模型不同,因此請手動配置。
▶RewriteRule 的旗標(例如 [L] 或 [R=301])會如何處理?
[L] 會在 rewrite 規則上加上 last 關鍵字;[R=301]/[R=permanent] 會產生永久重新導向;以 http(s):// 開頭的絕對目標位址會轉換為 location + return 區塊,並使用正確的狀態碼。
如果这个工具对你有帮助,请考虑请我喝杯咖啡。
請幫我買杯咖啡吧。