网站改版html页面 NGINX 借用伪静态和PHP脚本 实现301重定向跳转

原来的旧网站的每个文章的网址是这样的,它包含了动态参数:

www.1.com/page25.html?article_id=23

www.1.com/page25.html?article_id=55

改版后,网址随机,比如

www.1.com/news/123.html

方案一,在新服务器上,建立一个page25.html的静态文件,让他支持PHP脚本,然后在脚本中进行301跳转

方案二,在nginx服务器上,所有文章进行伪静态或301跳转.但是测试发现.旧网址中包含动态参数.没法直接跳转.

方案三,不管原来的page25.html是纯静态,还是伪静态,对搜索引擎都是无感的.所以.我们假设原来的page25.html就是伪静态的

然后,在新服务器上,直接建立一个page25.php的跳转文件.

并在服务器上设置伪静态.规则如下,

注意,除了page25需要传递动态参数外,其它页面,直接301永久重定向,所以才用了这个标志:permanent.

旧网址与新网址一一对应,需要手工整理总结了.

javascript 复制代码
rewrite ^/page25\.html.*$ /page25.php?article_id=$1 last;
rewrite ^/page30\.html$ /about/ permanent;
rewrite ^/page26\.html$ /download/ permanent;
rewrite ^/page24\.html$ /news/ permanent;
rewrite ^/page7\.html$ /sell/ permanent;
rewrite ^/page4\.html$ /product/ permanent;

然后,page25.php文件,里面的内容如下:

php 复制代码
<?php
// 目标URL
$u =(int)$_GET['article_id'];
 if($u==23){
 $url='https://www.2.com/news/20250325/276.html';}
elseif($u==24){$url='https://www.2.com/news/20250325/277.html';}
elseif($u==70){$url='https://www.2.com/news/2025/0325/287.html';}
elseif($u==111){$url='https://www.2.com/news/2025/0326/308.html';}
else{$url='https://www.2.com/news/';}
// 设置HTTP状态码为301
header('HTTP/1.1 301 Moved Permanently');
// 告诉浏览器新的URL位置
	header('Location: ' . $url); 
// 终止脚本执行
exit();
?>
相关推荐
dog2504 小时前
细看高维空间中距离度量失效
开发语言·php
二等饼干~za8986685 小时前
源码可控:云罗 GEO 源头工厂,开源搭建 + 二次开发全链路解决方案
服务器·开发语言·开源·php·音视频·ai-native
zhanghongbin015 小时前
本地持久化:网络故障数据保护
服务器·网络·php
软件开发技术5 小时前
最新版310版本绿豆UI9+后台源码+TV版APK+手机版APK
php
liliangcsdn6 小时前
sentence-transformer如何离线加载和使用模型
开发语言·前端·php
tryCbest6 小时前
Nginx常用操作命令-Linux和Windows系统
linux·windows·nginx
AI应用实战 | RE7 小时前
011、向量数据库入门:Embeddings原理与ChromaDB实战
开发语言·数据库·langchain·php
KKKlucifer8 小时前
零信任融合 4A 平台,构建全域身份动态可信管控体系
开发语言·php
TE-茶叶蛋9 小时前
在 ThinkPHP 5.1 项目中创建一个新的模块
php
无心使然云中漫步9 小时前
ArcGis常用服务介绍及Arcgis,Openlayers,Leaflet加载
开发语言·arcgis·php