惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

W-Blog - 分享兴趣,记录生活 - SEO优化

网站 SEO 全解析:从零基础入门到实战精通 网站标题(Title Tag)优化指南:如何精准捕捉搜索流量 多IP站群服务器:提升网站SEO效果与降低风险的理想选择 SEO优化与自然流量:提升网站访问量的关键策略 SEO基础知识分享(下篇):影响搜索引擎排名的不利因素 SEO基础知识分享(中篇):SEO常用搜索引擎高级指令解析 SEO基础知识分享(上篇):SEO基础名词全解析 是否应该将所有 404 页面重定向到主页? 导致网站SEO过度优化的原因
Nginx 301重定向:SEO优化、域名迁移与多场景高效配置
Patmon · 2021-04-21 · via W-Blog - 分享兴趣,记录生活 - SEO优化

98814.jpg

在Nginx中设置301永久重定向是常见的SEO优化、网站迁移或域名切换操作。以下是不同场景的详细配置指南:

一、基础301重定向配置

  1. 整站跳转(旧域名 → 新域名)

# 处理HTTP请求server {    listen 80;    server_name old-domain.com www.old-domain.com;    return 301 https://new-domain.com$request_uri;}# 处理HTTPS请求(需保留SSL证书)server {    listen 443 ssl;    server_name old-domain.com www.old-domain.com;    ssl_certificate /path/to/old-domain.crt;    ssl_certificate_key /path/to/old-domain.key;    return 301 https://new-domain.com$request_uri;}

说明:

  • 分开处理HTTP(80端口)和HTTPS(443端口)请求。

  • 旧域名的HTTPS配置需保留有效SSL证书,否则浏览器会报证书错误。

  1. 单页面跳转(旧URL → 新URL)
location = /old-page.html {    return 301 https://new-domain.com/new-page.html;}

说明:

  • 使用 = 实现精确匹配,避免模糊匹配问题。

二、高级场景配置

  1. 路径模式匹配

location ~ ^/blog/(2020)/(.*)$ {    return 301 https://new-domain.com/archive/$1/$2;}

   说明:

  • 将 /blog/2020/xxx 重定向到 /archive/2020/xxx

  • $1 和 $2 分别对应正则中的捕获组。

 2. 带参数保留跳转

location /product {    if ($arg_id ~ ^(\d+)$) {        return 301 /new-product/$1$is_args$args;    }    return 301 https://new-domain.com/; # 默认跳转}

说明:

  • 使用正则验证id参数为数字,$1为匹配结果。

  • $is_args$args 保留原始URL参数(如?color=red)。

3. 多域名合并跳转

server {    listen 80;    listen 443 ssl;    server_name old1.com old2.com www.old1.com;    ssl_certificate /path/to/cert.pem; # 若需处理HTTPS    return 301 https://main-domain.com$request_uri;}

说明:

  • 统一处理多个旧域名的HTTP/HTTPS请求。

三、特殊协议处理

1. HTTP强制跳转HTTPS

server {    listen 80;    server_name example.com;    return 301 https://example.com$request_uri;}

2. 非www跳转www 2. 非 www 跳转

# HTTP跳转server {    listen 80;    server_name example.com;    return 301 https://www.example.com$request_uri;}# HTTPS跳转(防止HTTPS非www直接访问)server {    listen 443 ssl;    server_name example.com;    ssl_certificate /path/to/cert.pem;    return 301 https://www.example.com$request_uri;}

四、调试与验证
1. 配置检查:

nginx -t        # 检查语法nginx -s reload # 重载配置

2. CURL测试:

curl -I http://old-domain.com/old-page# 检查响应头中的 Location 和状态码

五、注意事项

  1. SSL证书:

    • 旧域名的HTTPS重定向需配置有效证书,否则用户会先看到证书错误。

  2. 循环跳转:

    • 避免 www → non-www → www 循环,确保重定向逻辑单向。

  3. 参数处理:

    • 使用 $is_args$args 保留原参数(如 ?key=value)。

  4. 性能优化:

    • 优先用 return 而非 rewrite,减少正则复杂度。

  5. SEO建议:

    • 在Google Search Console提交新域名,并确保内容一致性。

完整配置示例:多条件重定向

# 多条件重定向server {    listen 80;    server_name legacy-site.com www.legacy-site.com;    # 旧博客按年份归档    location ~ ^/blog/(2019|2020)/(.*)$ {        return 301 https://new-site.com/archive/$1/$2;    }    # 商品ID跳转(带参数验证)    location /product {        if ($arg_id ~ ^(\d+)$) {            return 301 https://new-site.com/item/$1;        }        return 301 https://new-site.com/; # 默认跳转    }    # 全站默认跳转    return 301 https://new-site.com$request_uri;}

通过以上配置,可确保重定向高效、准确,同时规避常见陷阱。