.htaccess 中的 Rewrite 相关规则,或 Nginx location 里的 rewrite / 常见 if 块,点击对应按钮生成 Nginx 或 Apache 写法。复杂条件、变量与模块指令会标注为「未支持」或注释,请人工核对。
Apache 侧需在服务器配置中启用 RewriteEngine On(本工具在 Nginx→Apache 时仅在出现 RewriteRule 时注释提示)。Nginx 侧仅解析常见单行 rewrite … last|break|permanent|redirect; 以及 if (!-f/-d $request_filename) 包裹的 rewrite。
在换 Web 服务器或迁移站点时,经常要把 Apache 的 .htaccess 规则改写成 Nginx 的 rewrite,或反向把 Nginx 配置改回 Apache 的 RewriteRule。本工具在浏览器里完成常见伪静态片段的双向互转,减少手工对照时间。
使用步骤:在「源规则」文本框粘贴规则,选择Apache → Nginx或Nginx → Apache,点击「开始转换」,在「转换结果」中查看或复制。工具会忽略注释与空行,并对暂不支持的指令输出注释说明,部署前请在测试环境验证。
Apache 侧支持示例包括 RewriteCond(如 %{REQUEST_FILENAME} 的 !-f / !-d)、RewriteRule、DirectoryIndex、Options -Indexes、ErrorDocument、部分 Redirect。Nginx 侧支持单行 rewrite … last|break|permanent|redirect; 以及常见的 if (!-f $request_filename) / if (!-d $request_filename) 包裹的 rewrite,并可反向生成带 RewriteCond 的 Apache 写法。
以下为 Nginx 单行 rewrite 与工具输出方向的示意(实际结果以你粘贴内容为准):
rewrite ^/article/([0-9]+)\.html$ /article.php?id=$1 last;
对应 Apache 方向会近似生成 RewriteRule 与 [L] 等标志;301/302 会映射为 permanent/redirect 或 [R=301,L]/[R=302,L]。RewriteBase、[NC]、复杂 RewriteCond、Nginx 其他 if 变量等仍需人工调整,不适合作为唯一权威配置来源。
工具功能特点
- 一键转换:支持 Apache Rewrite 规则快速转化为 Nginx 格式;
-
语法识别强大:适配常见的
RewriteRule、重定向跳转、正则参数捕获; - 即时预览输出:转换结果实时显示在下方区域,可直接复制粘贴;
- 安全纯前端:无需上传配置文件,转换过程本地完成;
-
支持批量转换:支持粘贴多行 Apache 规则同时处理,适合整段
.htaccess语法迁移。
使用示例
假设你原有的 Apache 规则如下:
RewriteEngine On
RewriteRule ^about$ about.html [L]
RewriteRule ^tool/([0-9]+)$ tool.php?id=$1 [L]
Redirect 301 /old-page https://www.46.la/new-page
将其粘贴到工具中后,自动生成如下 Nginx 规则:
rewrite ^/about$ /about.html last;
rewrite ^/tool/([0-9]+)$ /tool.php?id=$1 last;
return 301 https://www.46.la/new-page;
是否很方便?再也不需要去查文档一条条对照修改了!
常见支持语法类型
| 类型 | 示例 | 转换后 |
|---|---|---|
| 基础路径重写 |
RewriteRule ^abc$ xyz.html [L]
|
rewrite ^/abc$ /xyz.html last;
|
| 参数捕获(正则) |
RewriteRule ^post/([0-9]+)$ post.php?id=$1
|
rewrite ^/post/([0-9]+)$ /post.php?id=$1 last;
|
| 永久跳转(301) |
Redirect 301 /old /new
|
return 301 /new;
|
| 外链重定向 |
Redirect 302 /go https://46.la
|
return 302 https://46.la;
|
使用小贴士
- Apache 的 RewriteEngine On 在 Nginx 中不需要;
- Nginx 的
rewrite一般使用last或permanent作为跳转指令; - 注意将路径补全为以
/开头的形式; - 多条规则建议分段测试,以防逻辑冲突;
- 若你的网站已部署 WordPress、Discuz、Laravel 等框架,转换时请参考对应官方的 Nginx 伪静态模板。
适合使用人群
- 网站运维人员:部署服务器时进行伪静态规则转换;
- 程序员/开发者:将本地开发用 Apache 配置迁移到 nginx;
- 建站用户:从虚拟主机迁移到云服务器或 CDN;
- SEO 优化师:配置301重定向跳转逻辑;
- 工具开发者:需要自动化生成 Nginx 规则的脚本支持。
常见问题 FAQ
Q:工具支持哪些 Apache 语法?
A:目前支持 RewriteRule、Redirect、带参数的规则、正则表达式、301/302等常见语法。
Q:转换后的语句可以直接复制到 nginx.conf 吗?
A:可以。推荐放在 location / {} 块内或 server 配置中使用。
Q:是否支持复杂条件判断?
A:本工具主要支持简单常见的语法,若你使用复杂的 RewriteCond 等结构,建议结合手动调整。
