Home  >  Article  >  Backend Development  >  PHP matches umlauts to make 404 pages more intelligent_PHP Tutorial

PHP matches umlauts to make 404 pages more intelligent_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:19844browse

创建您自己的 404 错识消息处理程序,为站点内容提供有用的链接和重定向。使用变音匹配(metaphone matching)和一个简单的加权记分文件为输入错误、拼写错误和无效链接生成重定向建议。根据 Web 站点的内容和首选重定向位置定制建议。捕获传入 URL 请求中的各种错误,并通过处理纠正其中的目录、脚本和 HTML 页面名称错误。

关于如何为 404 页面创建有效格式的教程比比皆是。这类教程大多建议在 404 页面中包含静态的建议链接,并将这些链接指向站点的公共区域,比如说首页、下载页面和站点的搜索引擎,前提是要有这些页面。404 页面普遍存在的问题是它们无法反映用户访问该站点的目的。本文将介绍如何构建一个建议生成器和一个方法,用于根据 Web 站点的内容提供更加有用的重定向链接。

现行的 404 处理程序允许我们为各种错误提供一些建议链接,比如说将用户指向站点目录。一些拼写校正程序(比如说 mod_speling ——— 没错,它只有一个 “l”)可用于纠正词典单词中的错误,从而将用户定向到正确的页面。本文中的代码将帮助您构建一个建议生成引擎,它可以根据 Web 站点的内容来处理在词典中无法找到的单词和目录链接。

我们考虑这样一个场景:您在电话会议中听到了一个 Web 页面名称,因此便尝试打开 blegs/DavSmath.html 链接。现行的拼写校正模块无法为此情况提供一个有用的链接。使用本文中的代码,您将能够生成一个 404 页面,并在其中显示建议的有效页面 /blogs/DaveSmith.html。

需求

本世纪生产的任何现代 PC 应该都足以编写和运行本文中的代码。如果您的 Web 页面含有超过 10,000 个不同的页面,那么可能需要大容量的内存、高性能的硬件或足够的耐心。

所提供的 Perl 和 CGI 脚本可以在多种 UNIX® 和 Windows® 平台上运行(请参阅 下载部分。虽然本文将使用 Apache 和一个 CGI 脚本作为建议引擎,但是所构建的工具应该能够在大多数 Web 服务器上正常运行。对于变音匹配,本文将引用 Michael Schwern 编写的 Text::Metaphone 模块。在开始之前,先通过喜好的 CPAN 镜像安装 Text::Metaphone 模块。请参阅 参考资料 获得下载信息。

Web 服务器页面和变音代码

针对输入和拼写错误提供替代建议的主要方法为变音匹配。与 Soundex 语音算法和一些其他算法类似,Metaphone 使用字母数字代码表示单词的发音。但是,与 Soundex 语音算法有所不同,构建语音代码的目的是匹配英文发音的语言可变性。因此,变音代码通常能够更加准确地表示特定的单词,并且为建议库的构建提供了理论基础。

考虑示例 Web 服务器目录中的下列文件。


清单 1. Web 服务器文件
               

以下为引用的内容:
./index.html
./survey.html
./search_tips.html
./about.html
./how.html
./why.html
./who.html
./NathanHarrington.html
./blogs/NathanHarrington.html
./blogs/DaveSmith.html
./blogs/MarkCappel.html

针对这些静态 HTML 文件,我们将使用 buildMetaphoneList.pl 程序为所有扩展名为 .html 的文件创建变音。


清单 2. buildMetaphoneList.pl
               

以下为引用的内容:
#!/usr/bin/perl -w
# buildMetaphoneList.pl - / split filename, 0 score, metaphones

use strict;
use File::Find;
use Text::Metaphone;

find(&htmlOnly,".");

sub htmlOnly
{
  if( $File::Find::name =~ /.html/ )
  {
    my $clipFname = $File::Find::name;
    $clipFname =~ s/.html//g;

    my @slParts = split /, $clipFname;
    shift(@slParts);

    print "$File::Find::name ### 0 ### ";
    for( @slParts ){ print Metaphone($_) . " " }
    print " ";

  }#if a matching .html file

}#htmlOnly sub

buildMetaphoneList.pl 程序只能处理扩展名为 .html 的文件,它将移除文件名中的 .html,然后为完整路径名称的各个部分生成变音。将 buildMetaPhoneList.pl 程序复制到 Web 服务器的根目录下,然后运行命令 perl buildMetaphoneList.pl > metaphonesScore.txt。对于清单 1 中的文件,相应的 metaphonesScore.txt 文件内容如清单 3 所示。

清单 3. metaphonesScore.txt
               

以下为引用的内容:
./index.html ### 0 ### INTKS
./survey.html ### 0 ### SRF
./search_tips.html ### 0 ### SRXTPS
./about.html ### 0 ### ABT
./how.html ### 0 ### H
./why.html ### 0 ### H
./who.html ### 0 ### H
./NathanHarrington.html ### 0 ### N0NHRNKTN
./blogs/NathanHarrington.html ### 0 ### BLKS N0NHRNKTN
./blogs/DaveSmith.html ### 0 ### BLKS TFSM0
./blogs/MarkCappel.html ### 0 ### BLKS MRKKPL

清单 3 中的每一行文字都显示了 Web 服务器根目录下的实际链接、默认作用域和变音代码。注意,how.html、 why.html 和 who.html 都解析为了相同的变音代码。要解决这个不明确的地方,需要修改作用域字段,让链接建议程序以指定的顺序向页面提供链接。比如说,将 “H” 变音条目修改为:

以下为引用的内容:
./how.html ### 100 ### H
./why.html ### 50 ### H
./who.html ### 0 ### H

This will create an intuitive link reordering and leave room for further scope modifications. The higher the number of scopes, the later in the order in which the same diacritical sound file (but in a different scope) is inserted. For example, if you add a hoo.html file list with scope 25, it will be above the who.html entry and below the why.html entry.

You can also use the scope field to distinguish files with the same name in different directories. For example, if the scope of the ./NathanHarrington.html line is changed to 100, then a request like nathenHorrington.html will list the ./NathanHarrington.html link before the ./blogs/NathanHarrington.html page.

When choosing a file scope, be sure to consider the statistical and logical access components of your website. It can be seen from the log file that users request the why.html page more frequently, but if you think how.html is more important to users, then just modify the corresponding scope value to correct the sorting.

Building a CGI 404 handler

Now that we have generated the appropriate umlauts and assigned them relevant scope values, the next step is to build the actual suggestion generator. Typically, 404 error messages are caused by typographical errors in the link or problems with the link itself. The suggestions generated by the following code will be created using three main tests: matching based on directory structure, matching using a combination of diacritical marks, and using "contains" matching when other methods fail. These three tests are designed to handle most 404 errors. The beginning of the MetaphoneSuggest CGI Perl script looks like this.

Listing 4. MetaphoneSuggest CGI Part 1
                                                   

The following is the quoted content: #!/usr/bin/perl -w
# MetaphoneSuggest - suggest links for typographical and other errors from 404s
use s

http://www.bkjia.com/PHPjc/531682.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531682.htmlTechArticleCreate your own 404 error handler to provide useful links and redirects to site content. Using metaphone matching and a simple weighted score file...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn