pbootcms规则下的链接301重定向到自定义URL

这个项目有个重新定义URL的功能,现在我发现重新定义URL后,原来链接依然可以访问,这对谷歌搜索引擎来说非常不友好,我需要怎么处理呢

apps\home\controller\IndexController.php

添加

php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use app\home\service\RedirectService;
private function getContent($data)
{
// 读取模板
if (! ! $sort = $this->model->getSort($data->scode)) {
if ($sort->contenttpl) {
$this->checkPageLevel($sort->gcode, $sort->gtype, $sort->gnote); // 检查栏目权限
$this->checkPageLevel($data->gcode, $data->gtype, $data->gnote); // 检查内容权限

// 使用服务层进行跳转判断
RedirectService::toStandardUrl( $data, 'content', $this->parser);

$content = parent::parser($this->htmldir . $sort->contenttpl); // 框架标签解析
$content = $this->parser->parserBefore($content); // CMS公共标签前置解析
$content = str_replace('{pboot:pagetitle}', $this->config('content_title') ?: '{content:title}-{sort:name}-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
$content = str_replace('{pboot:pagekeywords}', '{content:keywords}', $content);
$content = str_replace('{pboot:pagedescription}', '{content:description}', $content);
$content = $this->parser->parserPositionLabel($content, $sort->scode); // CMS当前位置标签解析
$content = $this->parser->parserSortLabel($content, $sort); // CMS分类信息标签解析
$content = $this->parser->parserCurrentContentLabel($content, $sort, $data); // CMS内容标签解析
$content = $this->parser->parserCommentLabel($content); // 文章评论
$content = $this->parser->parserAfter($content); // CMS公共标签后置解析
} else {
error('请到后台设置分类栏目内容页模板!');
}
} else {
_404('您访问内容的分类已经不存在,请核对后再试!');
}
$this->cache($content, true);
}

在home下面建service文件夹创建RedirectService.php

apps\home\service\RedirectService.php

php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace app\home\service;
use core\basic\Url;

class RedirectService
{
public static function toStandardUrl($data, $type = 'content', $parser = null)
{
$parser = $parser ?: new \app\home\controller\ParserController();
$request_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// 构造标准 URL
$new_url = $parser->parserLink(
$data->type,
$data->urlname,
$type,
$data->scode,
$data->sortfilename,
$data->id ?? '',
$data->filename
);

if ($request_url && $request_url != $new_url) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . $new_url);
exit;
}
}
}