typecho开启永久链接及旧链接301跳转解决404问题

前言

刚看到@风也温柔关于typecho永久链接的文章,也想给自己博客的链接结构修改一下,变得更简洁一点。
感谢@风也温柔大佬提出的解决方案。
本文章部分内容来自:https://eas1.cn/167.html
本文章讲解将链接结构从https://blog.zeruns.tech/index.php/archives/{cid}/修改为https://blog.zeruns.tech/archives/{cid}.html,其他链接结构修改方法可参考上面的链接,@风也温柔的文章没有讲这种链接结构的修改方法,我就补充一下。

开启永久链接

开启伪静态并添加下面的伪静态配置,这是Apache的配置,nginx自己找

<IfModule rewrite_module>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

typecho后台开启永久链接

修改旧链接跳转

打开主题的404.php文件,在合适的位置添加以下代码

<?php
$error_url = '//'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$id = strstr($error_url, 'index.php/archives');    //截取以posts开始的字符串
if($id != "" ||$id != null){//判断是否不等于空或者null
    $str = substr($id,18);
    $id2 = substr($str,-1);
    if($id2 != '/'){
      header('HTTP/1.1 301 Moved Permanently'); //添加301状态码
      header("location://".$_SERVER['HTTP_HOST']."/archives".$str.".html");//跳转页面
    }  //https://blog.zeruns.tech
    elseif($id2 == '/'){
     $nameid = substr($str,0,-1);
      header('HTTP/1.1 301 Moved Permanently'); //添加301状态码
      header("location://".$_SERVER['HTTP_HOST']."/archives".$nameid.".html");//跳转页面 
    }
}
?>

没学过PHP,不过感觉跟其他语言差不了多少,现学现用,有什么不对的地方可以在评论区提出来。

也可以在config.inc.php文件中

/** 载入路由器支持 */
require_once 'Typecho/Router.php';

的上面放上面的代码,不过在这放要去掉<?php?>

百度网站改版

打开:https://ziyuan.baidu.com/rewrite/

如果不知道如何填写正则规则,可以参考:https://ziyuan.baidu.com/college/courseinfo?id=267&page=5#h2_article_title22

推荐文章

高性价比和便宜的VPS/云服务器推荐:https://blog.zeruns.tech/archives/383.html
加入腾讯云自媒体分享计划获得代金券:https://blog.zeruns.tech/archives/441.html
typecho使用CDN后获取用户真实IP:https://blog.zeruns.tech/archives/414.html
Typecho外链转内链 跳转页面加广告:https://blog.zeruns.tech/archives/378.html
学生优惠权益大全:https://blog.zeruns.tech/archives/321.html

最后修改:2020 年 04 月 10 日
如果您觉得我的文章有帮助,请随意赞赏,赞赏有助于激发博主的热情,感谢!