需求:
網站域名下的子頁面路由改變,需要將訪問老路由的用戶重定向到新路由,例如:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/295373.html
標籤:其他
