Initial commit
Some checks failed
定时更新GitHub源插件 / 自动更新GitHub插件 (push) Has been cancelled

This commit is contained in:
chorblack
2026-03-07 11:19:25 +08:00
commit e75f275ef4
4484 changed files with 645480 additions and 0 deletions

120
RewriteRule/Plugin.php Normal file
View File

@@ -0,0 +1,120 @@
<?php
/**
* 在显示404之前使用规则重写
*
* @package RewriteRule
* @author laobubu
* @version 1.0.0
* @link http://laobubu.net
*/
class RewriteRule_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->error404Handle = array('RewriteRule_Plugin', 'error404Handle');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
$introduce = new Typecho_Widget_Helper_Layout("div");
$introduce->html(<<<EOI
<p>在 Typecho 正式显示 404 之前,此插件会遍历列表并作出跳转。</p>
<p>规则列表一行一个,格式为: Pattern Rewrite结果 [flags]</p>
<p>比如【 <code class="success">^\/(\d+)\/?$ /archives/$1 T</code> 】可以把 /12 以302重定向跳转到 /archivers/12 </p>
<p><a href="https://github.com/laobubu/Typecho_rewriterule">详细说明看这里</a></p>
EOI
);
$rules = new Typecho_Widget_Helper_Form_Element_Textarea('rules', NULL, '不符合书写规范的行可以当注释哦', _t('规则列表'));
$form->addInput($rules);
$form->addItem($introduce);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function error404Handle($that,$select) {
$rules = Typecho_Widget::widget('Widget_Options')->plugin('RewriteRule')->rules;
$rules = str_replace("\r","",$rules);
$rules = explode("\n",$rules);
$uri = $_SERVER['REQUEST_URI'];
$func_decorate_key = function($value) {return '$'.$value;};
$matcher_list = array('cid','slug','category');
foreach($rules as $i) {
$i = preg_replace("/\s+/","\t",trim($i));
$l = explode("\t",$i,3);
if (count($l)>=2) {
$flags = $l[2]?$l[2]:"";
$pattern = "/".$l[0]."/";
if (stristr($flags,'i')) $pattern.="i";
if (!preg_match($pattern,$uri,$match_out))
continue;
if (stristr($flags,'c')) {
$count_of_matcher = 0;
foreach ($matcher_list as $mli) {
if (isset($match_out[$mli])) {
$select = $select->where('typecho_contents.`'.$mli.'` = ?', $match_out[$mli]);
$count_of_matcher++;
}
}
if ($count_of_matcher==0) continue;
$that->slug = NULL;
$that->query($select);
$match_out['cid'] = $that->cid;
$match_out['slug'] = $that->slug;
$match_out['category'] = $that->category;
if (!$that->slug) continue;
}
$r1 = array_map($func_decorate_key,array_keys($match_out));
$r2 = array_values($match_out);
$newuri = str_replace($r1,$r2,$l[1]);
header(stristr($flags,'t') ? "HTTP/1.1 302 Found" : "HTTP/1.1 301 Moved Permanently");
header("Location: ".$newuri);
echo '<meta http-equiv="refresh" content="0; url='.$newuri.'" />';
echo "<a href=\"{$newuri}\">Click to continue</a>";
exit;
}
}
}
}

60
RewriteRule/README.md Normal file
View File

@@ -0,0 +1,60 @@
# RewriteRule for Typecho
一个能让你的 Typecho 在 404 显示之前重定向到其他地方的插件。
Blog: http://note.laobubu.net/archives/typecho-rewriterule
## 安装
将东西丢到 `usr/plugins/RewriteRule` 下即可,文件夹自建。
## 配置
访问后台-插件管理,启用 RewriteRule 后方可设置。
在输入框内输入跳转的规则,一行一个。每一行格式如
用来匹配的Pattern 新地址[ 可选的flags]
例如 `^\/(\d+)\/?$ /archives/$1 T` 会把诸如 `/27` 或者 `/27/` 的地址跳转到 `/archivers/27` 上(而且因为这里有 T 这个flag所以采用的是302临时重定向
## 一些有用的信息
### 可用的 flags
flags 的字母代号不分大小写,不需要间隔符。
| flag | 功能 | 注 |
| ---: | :--- | :--- |
| T | 这是一个302临时重定向。|若无此flag则默认301 |
| I | 为正则表达式打开i选项即不分大小写 | … |
| C | 按照条件查找文章。 | 这个有点复杂,注意看下面的 [应用举例1 之 分析那句规则](#分析那句规则) |
##应用举例
###让不同的永久链接共存
####一个故事
Yang 用 Typecho 搭建了一个 Blog写了一些文章但是由于一直没有设置永久链接因此他的文章地址一直是 `/archives/123/` 这样的。
有一天 Yang 为了提高网址易读性,他决定将永久链接改变为 `/archives/a-romantic-story/` 这样的格式,但是苦于过去的链接会失效,一直纠结怎么使得旧地址能跳到新地址上。
后来,他安装了 RewriteRule 这个插件,并添加了如下一句跳转规则:
^\/archives\/(?P<cid>\d+)\/$ /archives/$slug/ C
于是当访客访问 `/archives/123/` 这样的地址时, RewriteRule 便会查找对应文章的 slug 并引导访客到诸如 `/archives/a-romantic-story/` 这样的地址上。
####分析那句规则
1. 首先正则表达式 `^\/archives\/(?P<cid>\d+)\/` 会匹配先前提到的那种地址,并把其中的数字存为 `$cid` 变量。
2. 由于末尾加上了flag `C`,而且存在变量`$cid`,因此插件会搜索数据库查找对应文章。
1. 如果找到了,则扩展会补齐 `$cid, $slug, $category` 三个变量,规则指定的新地址 `/archives/$slug/`里面的变量名自然会被替换。
2. 如果没有查找到,则这句规则无效。要是后续的规则还是无效,则显示 404。
####关于 C 这个flag
这个 flag 会补全 `$cid, $slug, $category` 三个变量,只要 Pattern 里面搞到了至少一个上述变量就能工作。
要让正则给捕获组命名,写法有点难看,就像 `(?P<名字>正则表达式)` 这种。