Implement URL rewriting using httphandler

Source: Internet
Author: User

We often see a lot of website visitsArticleIs ***. HTML or ***. shtml (such as the Log Access Effect of this blog) does not exist on the server at the time. Why does this effect occur because the URL is rewritten on the Web server, by Rewriting the accessed URL into an internal access page according to a specific format, the advantage of the URL is that it is easy for users to understand, while the search engine can also better earn your website, of course, there are many other benefits. I will not introduce them here.

This article describes how to use httphandler in ASP. NET to rewrite a URL. For how it works, see here.ProgramAny URL can be processed, because I used a URL in the program for excessive consideration. Only a number is used to access the file name, and a new page is executed internally and data is output,CodeAs follows:

Public   Void Processrequest (httpcontext context)
{
Try
{
// Declarative request
Httprequest request = Context. request;
// Obtain the absolute path of the URL
String URL = Request. url. absolutepath;
// Start character interval of the accessed Web file
Int Regstart = URL. lastindexof ( " / " ) +   1 ;
// Declare whether all the Web file names are numbers
RegEx Reg =   New RegEx ( @" \ D + " );
// Match with regular expressions
If (Reg. ismatch (URL, regstart ))
{< br> // If the Web file name is a number, query related articles, run the specified page
context. server. execute ( " ~ /Permalink. aspx? Id = " + Reg. match (URL, regstart ). value);
}
}
Catch
{
Context. response. Redirect (context. Request. url. tostring ());
}
}

Of course, the first thing you need to do is to first create a class and inherit from ihttphandler. Then, compile the code into and compile it. To use this function in a web project, you must add the following statement in Web. config:

<Httphandlers>
<Add verb = "*" Path = "*. shtml" type = "httphandle. urlrewrite"/>
</Httphandlers>

In addition, you must configure the WEB Project in IIS. In the WEB Project Properties, on the Home Directory tab, change the execution permission to "script and executable file", and then enable the configuration, add the extension of the file format to be rewritten to the application extension.

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.