typecho開啓永久鏈接及舊鏈接301跳轉

typecho開啓永久鏈接及舊鏈接301跳轉解決404問題

原文:https://blog.zeruns.tech/archives/454.html

前言

剛看到@風也溫柔關於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");//跳轉頁面 
	}
}
?>

也可以在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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章