我博客某些链接访问被 301 重定向的奇怪现象

2022-10-19 17:48:55 +08:00
 brader

博客地址 https://brad.9iqu.cn

我的博客是使用 hexo 搭建的博客,有些链接会被重定向,不知道这是 hexo 的问题,还是我 nginx 配置的问题。

比如我博客里面有个页面 a 标签地址是 https://brad.9iqu.cn/tags ,然后它就非要给我 301 重定向到 https://brad.9iqu.cn/tags/ 。反正不带 /后缀的导航标签,基本都被重定向,感觉这样有点影响性能

下面是我的 nginx 配置

#重定向到 https 请求
server {
  listen          80;
  server_name     brad.9iqu.cn;
  return          301 https://$server_name$request_uri;
}

#https
server {
    listen       443 ssl http2 ; #SSL 访问端口号为 443
    server_name  brad.9iqu.cn;
    ssl_certificate cert/1_brad.9iqu.cn_bundle.crt; #证书文件
    ssl_certificate_key cert/2_brad.9iqu.cn.key; #私钥文件
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
    ssl_prefer_server_ciphers on;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root  /www/hexo-blog/public;
        index index.html index.htm index.php;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # deny access to .htaccess files, if Apache's document root
    location ~ /\.ht {
        deny  all;
    }
}
1276 次点击
所在节点    程序员
7 条回复
proxytoworld
2022-10-19 18:19:22 +08:00
有的 cdn 也会这样搞
brader
2022-10-19 18:21:27 +08:00
@proxytoworld 这个我没有上什么 cdn 的,我是在自己服务器自搭的
Wincer
2022-10-19 18:28:56 +08:00
加几行配置:

rewrite ^(.+)/+$ $1 permanent;
rewrite ^(.+)/index.html$ $1 permanent;
try_files $uri $uri/index.html =404;

另外,访问被重定向是默认的行为:在访问的 /tags 页面时,网站目录下并没有一个名叫 tags 的文件,而存在一个 tags 的目录,因此给你重定向到了目录下,展示了目录下的 index.html 文件的内容。
IvanLi127
2022-10-19 18:31:42 +08:00
你的 server 比较严谨,哈哈。他没把 index.html 给你吐出来就不错了
10935336
2022-10-19 19:02:17 +08:00
/tags 代表的是文件
/tags/ 代表的才是目录
eason1874
2022-10-20 14:48:46 +08:00
主流 CMS 都这样,会跳转,为了语义统一,区别像 #5 说的那样是文件和目录的区别

你内页链接统一用带斜杠的就不会有多余跳转了,其他人不小心访问到不带斜杆的就让它自动跳转,没什么负担,对性能影响可以忽略不计
brader
2022-10-20 18:29:59 +08:00
我把顶部导航的路径配置全部换成带 / 后缀的,现在好了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/888182

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX