johnroyer/url-normalizer

基于语法的URL规范化

2.1.1 2024-02-07 04:49 UTC

README

此URL规范化器是从 glenscott/url-normalizer 分支而来,并进行了一些修改

  • 升级PHPUnit到v9.x
  • 将跟踪参数作为选项移除(例如 utm_source, fbclid 等)

基于语法的URI规范化

该规范化基于RFC 3986规范https://tools.ietf.org/html/rfc3986

示例用法

require_once 'vendor/autoload.php';

$url = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d';
$un = new URL\Normalizer( $url );
echo $un->normalize();

// Result: 'example://a/b/c/%7Bfoo%7D'

规范化过程保留语义

例如,以下URL都是等效的。

  • HTTP://www.Example.com/http://www.example.com/
  • http://www.example.com/a%c2%b1bhttp://www.example.com/a%C2%B1b
  • http://www.example.com/%7Eusername/http://www.example.com/~username/
  • http://www.example.comhttp://www.example.com/
  • http://www.example.com:80/bar.htmlhttp://www.example.com/bar.html
  • http://www.example.com/../a/b/../c/./d.htmlhttp://www.example.com/a/c/d.html
  • http://www.example.com/?array[key]=valuehttp://www.example.com/?array%5Bkey%5D=value

执行的规范化

  1. 将方案和主机转换为小写
  2. 将转义序列中的字母大写
  3. 解码非保留字符的百分编码八位字节
  4. 添加尾部 /
  5. 移除默认端口
  6. 移除点段

有关这些规范化的更多信息,请参阅以下维基百科文章

http://en.wikipedia.org/wiki/URL_normalization#Normalizations_that_Preserve_Semantics

有关许可证信息,请参阅LICENSE文件。

选项

在规范化URL时,有两个选项默认禁用

  1. 移除空分隔符。启用此选项会将 http://www.example.com/? 规范化为 http://www.example.com/ 目前,此选项仅支持查询字符串分隔符(?)。
  2. 排序查询参数。启用此选项将按字母顺序对查询参数进行排序。例如,http://www.example.com/?c=3&b=2&a=1 变为 http://www.example.com/?a=1&b=2&c=3
  3. 移除跟踪参数。例如,https://example.com/?fbclid=xxxxx 变为 https://example.com/?