SoFunction
Updated on 2025-03-09

Rewrite URLReWriter with URLReWriter to implement any secondary domain name. New

Generally, friends who search on Baidu do not understand this but need it urgently. I think no matter how many languages ​​there are, they can't compare to a single code. So I passed on a small example I wrote when I was helping my friend today. The purpose of this small example is to rewrite any url (but not including secondary domain names. If you need secondary domain names, you can first understand the concept of url rewriting). The production process of this small project is as follows
1. You need to rewrite the url such as /viewnews/2009/3/. Rewrite it to /?id=2&year=2009&month=3
2. Download the Microsoft one and use this to rewrite the url.
Let's look at this url, /viewnews/2009/3/ (of course, it may be,). To rewrite, first, intercept the url request, then analyze the url at that time, and finally jump to the corresponding page. So our first step is to intercept the url request. For this purpose, a new class library is called the URL, and reference it in the class library. Create a new class, the code is as follows.
Copy the codeThe code is as follows:

namespace URL
{
public class myrewritter :
{
protected override void Rewrite(string requestedPath, HttpApplication app)
{
if (("viewnews/2009/3/"))
("/?id=2&year=2009&month=3");
else
("/");
}
}
}

You can see that this class inherits, and then adds our own logic to the Rewrite method. Now, as long as the current url request is viewnews/2009/3/, we will rewrite the page to /?id=2&year=2009&month=3.
This is just a simple example. In fact, the judgment of url is generally done using regular expressions. The correspondence between pages may need to be completed by querying the database.

Next, reference this class library in the website project. Then modify it, the following is mine
Copy the codeThe code is as follows:

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<>
<httpModules>
<add type="" name="URL" />
</httpModules>
<compilation debug="true" />
<authentication mode="Windows" />
</>
</configuration>

You can see that I have added a new httpModules. The purpose of this is that if there is a url request, it will first pass the request to the class you specified. The add line is the class that is to receive the request, and the following is the name of the dll where this class is located.
After adding this sentence, everything is almost over. Since we are dealing with the .html page, we need to configure the iis. Because the default processing engine does not care about .html. What we need to do is to use the processed program to process the html page. Open iis. Find your website, right-click to open the properties. Find the home directory (Home Directory), and open the configuration (Configuration) dialog box. Find the .aspx in the list of Application extensions (sorry, I don't know what this is called in the Chinese system) and click Edit (Edit...), and copy the content of the Executable box. For example, my name is: C:\WINDOWS\\Framework\v2.0.50727\aspnet_isapi.dll. Then click Add and in the opened dialog box, Executable pastes the path you just copied here, fills in the html extension, and then confirms to save. In this way, we hand over the request to the .html page to.
All are completed, compile the project (the Chinese environment is called Generation), open url http://localhost/mytest/viewnews/2009/3/ to see if it will be rewrite to http://localhost//?id=2&year=2009&month=3
Below are all the files of my examples. You can download and take a look. One sentence of code is more effective than reading ten sentences, right?
http://xiazai./200911/yuanma/
It should be noted that in my example, the url configuration of the web website is http://localhost:8011. You may need to change this configuration. The method is to right-click on the website project, open the properties, and then modify it in the web page.
As a beginner, you may feel annoyed by these things. If you don’t want to read a long article, the best way is to read my code. I have been learning flex recently and I am in a state of being unable to bear some new things. Spring is here, and everyone is more irritable, so calm down and take your time.
happy programming :)