需求:
网站域名下的子页面路由改变,需要将访问老路由的用户重定向到新路由,例如:
https://www.xxx.com?pid=123
指向https://www.xxx.com/dibiaoabc.html
。
两个后缀之间没有任何规律,且有多个页面需要匹配。
实现:
server文件内引入重定向配置,重定向配置中编辑rewrite规则,将原路径指向新路径。
server配置
server {
listen 80;
server_name localhost;
location /{
proxy_pass http://localhost:8080;
include /usr/local/nginx/conf/rewrite/*.conf;
}
}
rewrite.conf文件
if ($request_uri ~* ^(.*)pid=123(.*)$){
rewrite (.*) https://www.xxx.com/dibiaoabc.html? permanent;
}
if ($request_uri ~* ^(.*)pid=456(.*)$){
rewrite (.*) https://www.xxx.com/def321.html? permanent;
}
if ($request_uri ~* ^(.*)pid=789(.*)$){
rewrite (.*) https://www.xxx.com/fei111.html? permanent;
}
解释:$request_uri ~* ^(.*)pid=123(.*)$){ rewrite (.*) https://www.xxx.com/dibiaoabc.html? permanent;
# 匹配以pid=123结尾的请求路由
$request_uri ~* ^(.*)pid=123(.*)$)
# 重定向到(.*)和?之间的请求
rewrite (.*) https://www.xxx.com/dibiaoabc.html?
# 返回301永久重定向, 地址栏显示重定向后的url,爬虫更新url
permanent