A URL rewriting instance

Source: Internet
Author: User

Recently, I had a friend who wanted to rewrite the URL. I helped him with this. I looked back at the article I wrote and thought it was easy to understand. but now I think it is not easy to write. today, Baidu's urlrewriter found that my article was ranked second. to make it easier for more friends, I will write something to add.

In general, Baidu search is not familiar with this, but is in urgent need of use. I think, more languages than a code. so I wrote a small example for my friends today. the purpose of this small example is to rewrite any URL (but does not include a second-level domain name. If you need a second-level domain name, you can first understand the concept of URL rewriting ). the preparation process of this small project is as follows:

  1. Need? Id = 2 & year = 2009 & month = 3
  2. Download the Microsoft urlrewriter. dll and rewrite the URL.

Let's look at this URL, http://song2.cn/viewnews/2009/3/2.html (of course it can also be 3.html,6.html,7.html ).To rewrite the URL request, first intercept the URL request, analyze the current URL, and finally jump to the corresponding page. Therefore, the first step is to intercept the URL request.To create a new class library named URL, reference urlrewriter. dll in this class library to create a class named myrewritter. cs. The Code is as follows:

Namespace URL
{
Public class myrewritter: urlrewriter. basemodulerewriter
{
Protected override void rewrite (string requestedpath, httpapplication APP)
{
If (requestedpath. Contains ("viewnews/2009/3/2. html "))
App. Context. rewritepath ("/viewnews. aspx? Id = 2 & year = 2009 & month = 3 ");
Else
App. Context. rewritepath ("/here. aspx ");
}
}
}

We can see that this class inherits urlrewriter. basemodulerewriter, and then add our own logic to the rewrite method. now, as long as the current URL request is viewnews/2009/3/2. HTML, We will rewrite the page to/viewnews. aspx? Id = 2 & year = 2009 & month = 3.

This is just a simple example. In fact, the URL judgment is generally completed using a regular expression, and the correspondence between pages may need to be completed by querying the database.

Next, reference this class library in the website project. Then modify web. config. below is my web. config

You can see that I have added an httpmodules. this function is that if there is a URL request, Asp.net will first upload the request to the class you specified. add the row. The class before which the request is to be received is followed by the DLL name of the class.

After this sentence is added, everything will soon end.Because we want to process the web pages of. html, The iis.net processing engine cannot be configured. html. What we need to do is to process the HTML page by processing the ASP. Ent program.Open IIS. find your website and right-click to open properties. find the home directory and open the configuration dialog box. in the Application Extensions (sorry, I don't know what this is called in the Chinese system) list. aspx, click Edit ...), copy the content of the executable box. for example, my website is C:/Windows/Microsoft. net/framework/v2.0.50727/aspnet_isapi.dll. click Add. In the displayed dialog box, executablepaste the copied token to this page, and fill in extensionhtml. Then, it is guaranteed. In this case, we handed over the request for the .html page to Asp.net.

After all, compile the project (generated in the Chinese environment) and open the URL http: // localhost/mytest/viewnews/2009/3/2. HTML to see if it will be rewritten to http: // localhost // viewnews. aspx? Id = 2 & year = 2009 & month = 3

Below are all the files in my example. You can download them and check them out.Reading a code is more useful than reading 10 sentences., Right

In my example, the URL of a web site is configured as http: // localhost: 8011. You may need to change this configuration. you can right-click a website project to open properties. then modify it on the web page.

As a beginner, these things may be annoying. if you don't want to read a long article, the best way is to look at my code. I have been learning flex recently, and I am still unable to bear with some new things. when spring is approaching, everyone is excited. So let's calm down and take a while.

Source code download: rewrittertest.rar

For more advanced information, see here.:
Http://www.cnblogs.com/notus/archive/2007/03/13/673222.html

Classic Forum Communication:
Http://bbs.blueidea.com/thread-2918675-1-1.html

Link: http://www.blueidea.com/tech/program/2009/6551.asp

<? XML version = "1.0"?>

<Configuration>
<Appsettings/>
<Connectionstrings/>
<System. Web>
<Httpmodules>
<Add type = "url. myrewritter" name = "url"/>
</Httpmodules>
<Compilation DEBUG = "true"/>
<Authentication mode = "Windows"/>
</System. Web>
</Configuration>

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.