server {
listen 80;
server_name old-domain.com;
return 301 http://new-domain.com$request_uri;
}
server {
listen 80;
server_name temp-domain.com;
return 302 http://main-domain.com$request_uri;
}
location /old-path/ {
return 301 /new-path/;
}
location /old/ {
return 301 /new$request_uri;
}
location ~ ^/products/([0-9]+) {
return 301 /items/$1;
}
if ($http_user_agent ~* "bot|spider|crawler") {
return 301 http://bot-domain.com$request_uri;
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
location /private/ {
add_header X-Robots-Tag "noindex, nofollow";
# 或者返回403
# return 403;
}
location = /robots.txt {
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /admin/\nDisallow: /private/";
}
limit_req_zone $binary_remote_addr zone=crawlers:10m rate=1r/s;
location / {
limit_req zone=crawlers burst=5;
# 正常配置...
}
测试重定向:配置后使用curl测试:
curl -I http://yourdomain.com/old-path
避免重定向链:确保重定向一步到位,不要形成A→B→C的链条
保留查询参数:使用$request_uri
而非$uri
以保留原始查询参数
日志监控:在Nginx日志中监控重定向状态
SEO考虑:
重定向循环:
curl -L -v URL
跟踪重定向路径丢失查询参数:
$request_uri
而非$uri
性能优化:
通过以上配置,您可以灵活实现各种URL转发需求,同时控制搜索引擎的抓取行为,优化网站结构和SEO表现。