PHP match change tone makes 404 pages more intelligent _php Tutorial

Source: Internet
Author: User
Tags perl script

Create your own 404-error message handlers that provide useful links and redirects to your site's content. Use variable tone matching (metaphone matching) and a simple weighted scoring file to provide redirection recommendations for input errors, spelling errors, and invalid links. Customize recommendations based on your WEB site's content and preferred redirect location. Captures the various errors in the incoming URL request and corrects the directory, script, and HTML page name errors by processing them.

Tutorials abound about how to create a valid format for 404 pages. Most of these tutorials recommend that you include static suggestion links on the 404 page and point them to common areas of your site, such as the home page, download page, and site search engine, provided these pages are available. 404 pages The common problem is that they do not reflect the purpose of users accessing the site. This article describes how to build a proposal builder and a method to provide more useful redirect links based on the content of your Web site.

The current 404 handler allows us to provide some suggested links to various errors, such as pointing users to the site directory. Some spell correction programs (such as mod_speling ——— Yes, it has only one "L") can be used to correct errors in dictionary words, thus directing the user to the correct page. The code in this article will help you build a proposed build engine that can handle word and directory links that cannot be found in the dictionary based on the content of the Web site.

Let's consider a scenario where you hear a Web page name in a conference call and try to open the blegs/davsmath.html link. The current spelling correction module is unable to provide a useful link for this situation. Using the code in this article, you will be able to generate a 404 page and display the suggested valid page/blogs/davesmith.html in it.

Demand

Any modern PC produced in this century should be sufficient to write and run the code in this article. If your Web page contains more than 10,000 different pages, you may need large amounts of memory, high-performance hardware, or enough patience.

The Perl and CGI scripts provided can be run on a variety of UNIX® and Windows® platforms (see the Downloads section.) Although this article will use Apache and a CGI script as the recommendation engine, the tools you build should be able to function properly on most WEB servers. For variable tone matching, this article will refer to the Text::metaphone module written by Michael Schwern. Before you begin, install the Text::metaphone module with your preferred CPAN image. See Resources for download information.

Web Server Pages and tone codes

The main method for providing alternative suggestions for input and spelling errors is to match the tones. Similar to the Soundex speech algorithm and some other algorithms, Metaphone uses alphanumeric codes to denote the pronunciation of words. However, unlike the Soundex speech algorithm, the purpose of building a speech code is to match the language variability of English pronunciation. Therefore, the variable tone code can often more accurately represent a particular word, and provides a theoretical basis for building the proposed library.

Consider the following files in the sample Web server directory.


Listing 1. Web Server files

The following is the referenced content:
./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

For these static HTML files, we will use the buildmetaphonelist.pl program to create a variable tone for all files with the. HTML extension.


Listing 2. buildmetaphonelist.pl

The following is the referenced content:
#!/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

The buildmetaphonelist.pl program can only handle files with an. html extension, which removes. html from the file name and generates a tone for each part of the full path name. Copy the buildmetaphonelist.pl program to the root of the WEB server, and then run the command Perl buildmetaphonelist.pl > MetaphonesScore.txt. For the files in Listing 1, the corresponding MetaphonesScore.txt file contents are shown in Listing 3.

Listing 3. MetaphonesScore.txt

The following is the referenced content:
./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

Each line of text in Listing 3 shows the actual link, default scope, and tone code under the root of the WEB server. Note that how.html, why.html, and who.html all parse for the same tone code. To resolve this ambiguity, you need to modify the scope field so that the link suggestion program provides links to the page in the order specified. For example, change the "H" tone entry to:

The following is the referenced content:
./how.html # # # # H
./why.html # # # # # # H
./who.html # # # 0 # # # H

This creates an intuitive link reordering and leaves space for further modifications to the scope. The larger the number of scopes, the higher the order in which the same audio file (but different scopes) are inserted. For example, if you add a list of hoo.html files scoped to 25, it will be located above the who.html entry and below the why.html entry.

You can also use scope fields to differentiate between files with the same name as the directory. For example, if you change the scope of the./nathanharrington.html line to 100, a request like nathenhorrington.html will have the./nathanharrington.html link listed in the./blogs/ Before the nathanharrington.html page.

When choosing the scope of a file, it is important to consider the statistical and logical access components of the Web site. As you can see from the log file, the user's request for the why.html page is more frequent, but if you think how.html is more important to the user, then just modify the corresponding scope value to correct the sort.

Building a CGI 404 handler

We have generated the appropriate tones and specified the relevant scope values for them, and the next step is to build the actual recommendation generator. Typically, the cause of the 404 error message is a link input error or a problem with the link itself. The following code-generated recommendations are created with the following three main tests: match according to the directory structure, use a mix of tones, and use the "include" match when other methods fail. These three types of tests are designed to handle most 404 errors. The starting part of the Metaphonesuggest CGI Perl script is shown below.

Listing 4. Metaphonesuggest CGI Part 1th

#!/usr/bin/perl-w
# metaphonesuggest- Suggest links for typographical and the errors from 404s
use s

http://www.bkjia.com/phpjc/531682.html www.bkjia.com true http://www.bkjia.com/phpjc/531682.html techarticle Create your own 404 mistaken message handlers, Provides useful links and redirects for site content. Use variable tone matching (metaphone matching) and a simple weighted scoring file for ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.