glenscott/url-normalizer

基于语法的URL标准化

1.4.0 2015-06-11 16:06 UTC

This package is not auto-updated.

Last update: 2024-09-10 16:01:55 UTC


README

本工具根据RFC 3986规范对URI进行标准化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

待办事项

添加更多基于方案的标准化步骤,具体请参阅RFC的第6.2.3节。