在线htaccess转Nginx工具
如果这个工具帮到了你,可以请作者喝杯咖啡 ☕
将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 配置,开始无缝迁移吧。