SoFunction
Updated on 2025-03-09

Rewrite URLReWriter with url to implement any secondary domain name page 1/2

I haven't written a technical article for a long time. If you don't understand, read a few more articles, sweat, or reply later in the article (this is the most effective method), and I will try my best to help everyone answer questions.

Those who come to this article should know what a second-level domain name is, so I won’t say nonsense. But before discussing, you must first understand a problem of thought.
The problem that many friends have been unable to think clearly (I have been unable to understand a few days ago) is, after I type an address, why is this url rewritten?
Step 1: Type an address in the browser, for example, after clicking Enter, what happened?
To simplify the problem, I will explain it like this:
Step 2: First, the typed address is parsed and finally comes to a web server. It is handed over to IIS for processing. In the world of .net, IIS will hand over such a request to a web processor for processing. Finally, the web processor returns the processing results to the browser and displays them to the user.
Please do not ignore this problem. All things in the second step are done on the server side. When these things are carried out, the address on the browser on the user side will not change. Even when the web processor returns the processing results in the end, the address on the above will not change.
The URL you typed at the beginning just acts as a knock on the door. After the door is knocked, the function is over. Only your eyes can see the address, browser, server, etc., which do not know the address.
Then the problem to be understood is that the so-called url rewrite is just an insider that web developers know. The user has no idea what is going on. He believes that the address he typed is the result that should be displayed on the screen. That is to say, we control the content to be displayed behind the scenes.
The next thing to consider is how to control the displayed content?
From the process mentioned above, it is obvious that you need to make a move at the work of the web processor.

The simplest consideration is that the user typed in a simple address without any parameters, and then we changed this address to an address with parameters that meets the needs of the program, ?lover=notus, and finally processed.
The so-called url rewriting is at this step.
In .net terms, we need to register an httpmodule for the application to handle specific URLs.
Register httpmodule, in,
Process url, in the httpmodule program we provide

It's roughly equivalent to a program like this

// Use our httpmodule program to intercept the original url
String OriginalUrl=” ”;
// Process the original url and get the last needed url, the value is ?lover=notus
String FinalUrl=Rewrite(OriginalUrl);
// context resends the url internally to IIS processing
(FinalUrl);

Next, let's implement url rewrite.
Step 1: Determine which urls to be rewrite, that is, formulate rewrite rules
Step 2: Write httpmodule handler
Step 3: Integrate the written httpmodule into the web program and start working.

The above is the basic knowledge of url rewriting, and the process of implementing a second-level domain name using url rewriting is the same. Because whether it is a second-level domain name or a third-level domain name, it is a url address. As long as we intercept this url address, we can do things while processing it.

These jobs are quite troublesome, but there are experts on the Internet who have written such programs for us. See the following article:

/china/msdn/library/webservices//

/jzywh/archive/2005/09/29/

/jzywh/archive/2006/02/20/

The article is over.

During the implementation process, some problems will be encountered, mostly because I don’t get it carefully when reading the above article, but to be honest, it is not easy to read such a long article. Let me record some important problems. The last two problems are shown in the specific code to show how to deal with the rewrite target url to meet our requirements


Why do you have to use general analysis?
After reading many replies from friends, I think there may be a misunderstanding now, that is, this article about url rewriting is just to introduce some processing methods to you. As for general analysis or not, it is not important.
If you don’t need to implement any secondary domain name, there is no need to implement general analysis. You just need to kill the secondary domain name you need and then handle it in the url rewrite!
Step back a step back. If you don’t even need to implement a second-level domain name, just rewrite the url under a fixed domain name, you don’t need to modify the url rewriter of msdn. You can directly use it to implement a simple url rewrite. zyw’s modification of this project is just to obtain all urls for greater control. As we can see, the urlrewriter of msdn did not care about the domain name at the beginning
I first asked this question for the article because I have used it in my project recently. I wrote this article by the way when I was writing the document.



What is Microsoft's URLRewriter? Where did you download this project?
This is a sample program provided in the previous article introducing URLRewriter in msdn. You can download it here
/china/msdn/library/webservices//


How to use these codes? Is it troublesome?
To be sure, there is no trouble, there are:
Download the code to your machine.
After installation, add the URLRewriter project to your own project
Modify the code according to the method given above
Configure and get started.


What is httpmodule?

A simple understanding is a program that handles http requests together
For more detailed understanding, please refer to the SDK documentation.


How to implement general analysis?

First, add a *. secondary domain name to the domain name service provider and point to your server IP
Then, create a site in IIS, and the host header of this site is left blank, and the port is generally 80. This site is the default website for the entire server port 80.
Add a wildcard application map to this site (IIS site properties -> Home directory -> Configuration). The purpose of this mapping is to allow ISAPI to take over any secondary domain sites that are not explicitly specified in IIS.


What happened when you randomly enter a secondary domain name?
When IIS detects that the incoming url is a secondary domain name, it will first check whether there is a site on IIS that has the secondary domain name registered. If so, it will be transferred to this site. Otherwise, it will be transferred to the default site. This default site is the site where the host header was empty before configured. Therefore, there can only be one site with the host header empty on a port.
We have set ISAPI to take over these children without homes. Write programs, analyze incoming urls, and perform rewrites.
12Next pageRead the full text