首页 > 解决方案 > Nginx 忽略尾随斜杠,如果丢失不重定向

问题描述

使用我当前的配置,以下 URL 会触发对 SEO 非常不利的重定向。

http://localhost/blog 重定向到 -> http://localhost/blog/

这是我的 conf.d/default.conf 文件

server {
  listen 80 default_server;
  server_name localhost;
  server_name_in_redirect on;

  location / {
    add_header Cache-Control "public";
    expires 1y;
    root /work/front-page/dist/browser;
  }

}

我希望这两个位置都提供状态 200,或者至少要反转重定向

http://localhost/blog/重定向到 -> http://localhost/blog

编辑:把

try_files $uri $uri/index.html =404;

似乎解决了这个问题,谢谢@Richard Smith

这是完整的工作配置,我也使用 http -> https 重定向对其进行了测试。

server {
  listen 80 default_server;
  server_name localhost;
  server_name_in_redirect on;

  location / {
    add_header Cache-Control "public";
    expires 1y;
    try_files $uri $uri/index.html =404;
    root /work/front-page/dist/browser;
  }

}

标签: nginxredirect

解决方案


推荐阅读