truecastdesign / jsonredirects
基于 JSON 文件的重定向,具有精确匹配和通配符 * 匹配功能,可以更改较高级别的路径及其子类别,以便子类别也能进行重定向。
v2.0.0
2023-07-25 18:54 UTC
Requires
- php: >=5.5.0
This package is auto-updated.
Last update: 2024-09-25 21:23:47 UTC
README
基于 JSON 文件的重定向,具有精确匹配和通配符 * 匹配功能,可以更改较高级别的路径及其子类别,以便子类别也能进行重定向。
使用
Truecast\Redirects::redirect(['request'=>$_SERVER['REQUEST_URI'], 'lookup'=>BP.'/redirects.json', 'type'=>'301']);
JSON 文件
{
"path1/path2/":"path3/",
"path4/path5/*":"path4/path6/*",
"path7/path9/*":"path7/path10/",
}
第一行: 简单重定向。键:请求 URI 和值:重定向 URI
第二行: 匹配 "path4/path5/path8/" 重定向到 "path4/path6/path8/" 与请求中 * 匹配的路径被移动到重定向 URI 的末尾,代替 *
第三行: 匹配 "path7/path9/path8/" 重定向到 "path7/path10/" 与请求中 * 匹配的路径不会被移动到重定向 URI 的末尾,因为重定向中没有 * 符号
使用 PHP 编辑 JSON 文件
添加重定向
$Redirects = new App\Redirects(BP.'/config/redirects.json'); $Redirects->add($App->request->post->fromUrl, $App->request->post->toUrl);
删除重定向
$Redirects = new App\Redirects(BP.'/config/redirects.json'); $Redirects->remove($App->request->delete->fromUrl);
获取重定向列表
$Redirects = new App\Redirects(BP.'/config/redirects.json'); $list = $Redirects->getRedirectsList(); // array ['from-url'=>'to-url', 'from-url'=>'to-url']
清洁 URL
$Redirects = new App\Redirects(BP.'/config/redirects.json'); $cleanedURL = $Redirects->cleanUrl($url);
注意
此代码在顶部路由脚本中运行良好,在其他自定义路由检查之前。
它仅设计用于匹配 URL 的末尾,而不是中间部分,如 path/*/path。如果这有帮助,请告诉我,我将看看我能做什么。
谢谢!
希望这有帮助。