
6

Nginx404错误设置301重定向到其它页面的办法
source link: https://www.huhexian.com/20631.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx404错误设置301重定向到其它页面的办法
2021-12-3111:59:381 543字
众所周知,404 错误直接影响到我们网站的 SEO,因为这与 SEO 有关。而子凡将分享一个技巧,通过该技巧,你可以通过允许 404 错误页面并且将其 301 重定向到主页或者其它网站页面来提高网站的 SEO 性能。
当然这里子凡举个实际中很常见的例子,当我们的网站做了域名改版后,当时又需要用到老域名重新来建站的时候,这时候如果用老玉米重新建新站,就可以利用 404 的方式做 301 重定向,这样就保证了老域名之前的改版,也兼容了重新建站。
话不多说,因为子凡自己的所有服务器都是 Nginx 环境,所有以下就是适合 Nginx 环境 404 错误设置 301 跳转的方法。
- # 404 to 301 跳转到首页
- error_page 404 = @notfound;
- location @notfound {
- return 301 /;
下面的代码就是跳转到对应网站的对应页面,用于兼容网站做域名改版。
- # 404 to 301 跳转到泪雪网
- error_page 404 = @notfound;
- location @notfound {
- return 301 https://www.leixue.com$request_uri;
代码中的$request_uri 就是页面路径,删除这个变量就是跳转到泪雪网首页了。当然也可以使用 302 临时重定向,只需要将代码中的 301 改为 302 即可。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK