如果這個工具幫到了你,可以請作者喝杯咖啡 ☕
將Apache .htaccess重寫規則、重定向和訪問控制指令快速轉換為Nginx配置,方便網站遷移。
內容僅在瀏覽器中轉換,不會上傳。支援常見重寫、重定向、錯誤頁、目錄索引和響應頭指令。
輸入 .htaccess 配置後,將在這裡實時生成 Nginx 配置
把網站從Apache遷移到Nginx時,最讓人頭疼的就是.htaccess裡的重寫規則不能直接用了。一名站長手動轉換了整整50條URL重定向,花費半天時間——而用我們這個轉換器,貼上進去就能即時得到Nginx配置。這個工具專門處理Apache mod_rewrite的重寫規則、RewriteCond條件、301/302重定向,以及常見的訪問控制指令,保障切換平穩過渡。
下表列出了幾種最常用的.htaccess指令以及轉換後對應的Nginx配置。你可以直接在轉換器裡貼上左側的.htaccess規則,驗證轉換結果。
| .htaccess 規則 | Nginx 配置 |
|---|---|
RewriteRule ^old$ /new [R=301,L] | rewrite ^/old$ /new permanent; |
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] | if ($host = 'www.example.com') { rewrite ^/(.*)$ http://example.com/$1 permanent; } |
RewriteCond %{REQUEST_FILENAME} !-f | try_files $uri $uri/ /index.php?$args; |
<FilesMatch "\.(htaccess|htpasswd)$"> deny from all </FilesMatch> | location ~* \.(htaccess|htpasswd)$ { deny all; } |
開啟轉換頁面,你會看到兩個主要區域:左側是輸入框,右側是輸出框。操作步驟如下:
RewriteRule ^about$ /about-us.html [R=301,L]。rewrite ^/about$ /about-us.html permanent;。如果轉換過程中遇到不支援的指令,輸出框頂部會顯示警告資訊,指出哪些行被跳過或需要手動調整。
假設你有一個WordPress站點,.htaccess中典型的固定連結規則如下:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress將其貼上到轉換器左側輸入框,點選“轉換”。右側輸出的Nginx配置為:
# WordPress permalinks
location / {
try_files $uri $uri/ /index.php?$args;
}
轉換器識別出這組規則的核心意圖是“如果請求的檔案或目錄不存在,交給index.php處理”,並自動合併為Nginx高效的try_files指令,避免了低效的if判斷。你在Nginx配置中直接使用這段配置,即可實現相同的偽靜態效果。
場景1:301重定向舊頁面
輸入:
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]輸出:rewrite ^/old-page\.html$ /new-page.html permanent;場景2:基於域名的條件跳轉
輸入:
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]輸出:if ($host = 'olddomain.com') {
rewrite ^/(.*)$ http://newdomain.com/$1 permanent;
}場景3:禁止訪問特定檔案
輸入:
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>輸出:location ~* \.(htaccess|htpasswd|ini|log|sh)$ {
deny all;
}這些例子你都可以在轉換器裡直接測試,貼上後立即看到結果。
現在,把你的 .htaccess 規則貼上到上方的轉換器中,立即獲得 Nginx 配置,開始無縫遷移吧。