You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Nginx中实现无浏览器地址栏变更的请求重写并隐藏html后缀

Nginx无后缀访问HTML文件(地址栏无跳转)实现方案

实现原理

你之前看到的rewrite方案会触发地址栏跳转,是因为配置里用了redirect(302临时跳转)或permanent(301永久跳转)标记。只要使用内部重写规则,就可以实现无感知的路径映射,地址栏不会发生变化。

适配你当前Bitnami环境的配置修改

你选择的/opt/bitnami/apps/wordpress/conf/nginx-app.conf配置路径是正确的,这里提供两种兼容现有WordPress规则的修改方案:

方案1(推荐):try_files实现更优雅,性能更高

将原配置中index index.php index.html index.htm;之后的4个if判断块替换为如下配置:

location / {
    # 优先匹配原路径,再匹配加.html后缀的文件,再匹配目录,最后走WordPress伪静态
    try_files $uri $uri.html $uri/ @wordpress;
}

location @wordpress {
    if ($request_uri !~ "^/phpmyadmin.*$")
    {
      set $test  A;
    }
    if ($request_uri !~ "^/bitnami.*$")
    {
      set $test  "${test}B";
    }
    if (!-e $request_filename)
    {
      set $test  "${test}C";
    }
    if ($test = ABC) {
      rewrite ^/(.+)$ /index.php?q=$1 last;
    }
}

方案2(轻量修改):不改动原有规则结构

直接在index index.php index.html index.htm;行之后、第一个if判断之前添加如下规则即可:

# 当对应.html文件存在时,内部重写路径,无跳转
if (-f $request_filename.html) {
    rewrite ^(.*)$ $1.html last;
}

生效步骤

  1. 配置修改完成后,先执行语法校验,避免配置错误导致服务不可用:
    sh sudo /opt/bitnami/nginx/sbin/nginx -t
  2. 校验返回success后,重载Nginx生效:
    sh sudo /opt/bitnami/ctlscript.sh restart nginx

内容的提问来源于stack exchange,提问作者HelloWorld

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 22:39:03