已完成URL Rewriting的站点如何将指定旧URL重定向到对应新URL
301重定向配置方案
所有方案均采用301永久重定向配置,不会丢失谷歌已有的搜索排名权重,满足SEO要求。
核心注意事项
- 禁止使用302临时重定向,否则会导致排名权重丢失
- 配置完成后需逐一验证跳转有效性,可通过
curl -I 旧URL命令查看返回的状态码是否为301,也可使用浏览器隐身模式访问测试
方案1:Apache服务器(.htaccess配置)
前提:已开启mod_rewrite模块,站点根目录存在可编辑的.htaccess文件
添加以下规则即可:
RewriteEngine On # 第1条(需求给出的示例映射) RewriteCond %{QUERY_STRING} ^CId=1211$ RewriteRule ^ViewCategory\.php$ http://www.example.com/Carrom-Boards.html? [L,R=301] # 第2条自定义映射 RewriteCond %{QUERY_STRING} ^CId=1212$ RewriteRule ^ViewCategory\.php$ http://www.example.com/Chess-Sets.html? [L,R=301] # 第3条自定义映射 RewriteCond %{QUERY_STRING} ^CId=1213$ RewriteRule ^ViewCategory\.php$ http://www.example.com/Darts-Boards.html? [L,R=301] # 第4条自定义映射 RewriteCond %{QUERY_STRING} ^CId=1214$ RewriteRule ^ViewCategory\.php$ http://www.example.com/Poker-Sets.html? [L,R=301] # 第5条自定义映射 RewriteCond %{QUERY_STRING} ^CId=1215$ RewriteRule ^ViewCategory\.php$ http://www.example.com/Table-Tennis-Sets.html? [L,R=301]
说明:规则末尾的?作用是清除旧URL的查询参数,避免新URL后附带无用的旧参数。
方案2:Nginx服务器配置
前提:可编辑站点对应的Nginx配置文件,规则添加到对应server块内
添加以下规则即可:
# 第1条(需求给出的示例映射) if ($query_string = "CId=1211") { rewrite ^/ViewCategory\.php$ http://www.example.com/Carrom-Boards.html? permanent; } # 第2条自定义映射 if ($query_string = "CId=1212") { rewrite ^/ViewCategory\.php$ http://www.example.com/Chess-Sets.html? permanent; } # 第3条自定义映射 if ($query_string = "CId=1213") { rewrite ^/ViewCategory\.php$ http://www.example.com/Darts-Boards.html? permanent; } # 第4条自定义映射 if ($query_string = "CId=1214") { rewrite ^/ViewCategory\.php$ http://www.example.com/Poker-Sets.html? permanent; } # 第5条自定义映射 if ($query_string = "CId=1215") { rewrite ^/ViewCategory\.php$ http://www.example.com/Table-Tennis-Sets.html? permanent; }
配置完成后执行nginx -s reload重载配置即可生效。
方案3:PHP代码实现(无法修改服务器配置时使用)
直接在旧站点的ViewCategory.php文件顶部添加以下代码即可:
<?php // 旧CID与新URL的映射关系,可根据实际需求修改 $redirectMap = [ 1211 => 'http://www.example.com/Carrom-Boards.html', 1212 => 'http://www.example.com/Chess-Sets.html', 1213 => 'http://www.example.com/Darts-Boards.html', 1214 => 'http://www.example.com/Poker-Sets.html', 1215 => 'http://www.example.com/Table-Tennis-Sets.html', ]; $cid = $_GET['CId'] ?? 0; if (isset($redirectMap[$cid])) { header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $redirectMap[$cid]); exit(); } // 原有页面的业务逻辑写在下方即可 ?>
内容的提问来源于stack exchange,提问作者Vinex Sports
相关产品推荐
相关产品推荐

