URL rewriting implements any second-level domain name or multi-level domain name

Source: Internet
Author: User
Brief Review:
Modification of Microsoft's urlrewrite can rewrite the URL. Here, the domain name must be rewritten to implement Http://1234.abc.com/To Http://www.abc.com/show.aspx? Id = 1234.
Step: 1. Your Domain Name Http://www.abc.com/Is pan-parsed, and a ing with the Host Header null is added to IIS;
2. Modify Microsoft's urlrewriter in two places
(1). basemodulerewriter. CS protected virtual void basemodulerewriter_authorizerequest (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
Rewrite (App. Request. url. absoluteuri, APP );
}

Replace app. Request. path with app. Request. url. absoluteuri.

(2). modulerewriter. CS

For (INT I = 0; I <rules. Count; I ++)
{
// Get the pattern to look for, and resolve the URL (convert ~ Into the appropriate directory)
String lookfor = "^" + rules [I]. lookfor + "$ ";

// Create a RegEx (note that ignorecase is set)
RegEx Re = new RegEx (lookfor, regexoptions. ignorecase );

// See if a match is found
If (Re. ismatch (requestedpath ))
{
// Match found-Do any replacement needed
String sendtourl = rewriterutils. resolveurl (App. Context. Request. applicationpath, re. Replace (requestedpath, rules [I]. sendto ));

// Log rewriting information to the trace object
App. Context. Trace. Write ("modulerewriter", "Rewriting URL to" + sendtourl );

// Rewrite the URL
Rewriterutils. rewriteurl (App. Context, sendtourl );
Break; // exit the For Loop
}
}

Set string lookfor = "^" + rewriterutils. resolveurl (App. Context. Request. applicationpath, rules [I]. lookfor) + "$ ";
Changed to string lookfor = "^" + rules [I]. lookfor + "$ ";

After completing these two changes, recompile the project and copy the generated DLL to the bin directory of your project.

3. Write the rewrite Regular Expression in Web. config.

<Rewriterrule>
<Lookfor> http: // (/d +)/. ABC/. com </lookfor>
<Sendto>/show. aspx? Id = $1 </sendto>
</Rewriterrule>

Note:
The problem has come out. Many people have done this to achieve domain name jump, but many people have switched to the home page, which is not lookfor/to/default. aspx problem, I also encountered this problem, you need to add a "/" rule in the rewrite rule at the end of lookfor to become: <rewriterrule>
<Lookfor> http: // (/d +)/. ABC/. com/</lookfor>
<Sendto>/show. aspx? Id = $1 </sendto>
</Rewriterrule>

Another problem (this problemUrlrewriter.rarMethod 2 is used to solve the problem), that is, the modified rewrite is unfriendly to the parameter. If the rewritten URL contains a parameter suchHttp://www.abc.com/news.html? Id = 1000404 is returned after rewriting. The following provides a solution for parameter interruption after Rewriting:
Method 1:
Modify the rewrite rule to make the regular expression accept the parameter: <rewriterrule>
<Lookfor> http: // (/d +)/. ABC/. com/news.html (. {0,}) </lookfor>
<Sendto>/show. aspx? Id = $1 </sendto>
</Rewriterrule>

Then you can match all your parameters to the news page, such as ID and paging, so that you can use the parameters normally on the page.

Method 2:
Modify basemodulerewriter. CS protected virtual void basemodulerewriter_authorizerequest (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
String Path = app. Request. url. absoluteuri;
If (path. Contains ("? "))
{
Path = path. Split ('? ') [0];
}
Rewrite (path, APP );}

With "?" The absolute URL of is split, only matching URLs without parameters.

Method 3:
Modify modulerewriter. CS for (INT I = 0; I <rules. Count; I ++)
{
// Get the pattern to look for, and resolve the URL (convert ~ Into the appropriate directory)
String lookfor = "^" + rules [I]. lookfor + "(. {0,}) $ ";

// Create a RegEx (note that ignorecase is set)
RegEx Re = new RegEx (lookfor, regexoptions. ignorecase );

// See if a match is found
If (Re. ismatch (requestedpath ))
{
// Match found-Do any replacement needed
String sendtourl = rewriterutils. resolveurl (App. Context. Request. applicationpath, re. Replace (requestedpath, rules [I]. sendto ));

// Log rewriting information to the trace object
App. Context. Trace. Write ("modulerewriter", "Rewriting URL to" + sendtourl );

// Rewrite the URL
Rewriterutils. rewriteurl (App. Context, sendtourl );
Break; // exit the For Loop
}
}

Set string lookfor = "^" + rules [I]. lookfor + "$"; changed to string lookfor = "^" + rules [I]. lookfor + "(. {0 ,}) $ ";

Note:

1. domain name resolution Problems
Entered Domain NameHttp://1234.abc.comThe browser prompts that the webpage cannot be found. First, you should confirm whether your domain name supports wildcard domain name resolution, that is, to make all the second-level and third-level domain names point to your server. Second, make sure that your site is the default site on the server, that is, a site with an empty Host header on port 80 can be accessed directly with an IP address.Http://1234.abc.comEither prompt the error message of your site, or correctly execute your defined urlrewrite, or display the homepage of your site.

2. The rewrite cannot be executed.
If you confirm that your domain name resolution is correct, but still cannot be rewritten, accessHttp://1234.abc.comThe system will prompt that the path "/" cannot be found ...,
If so, add the wildcard application ing of aspnet_isapi first (this step is required, sorry! Not mentioned in the previous article ).

Operation Method: IIS Site Properties> main directory> Configuration

Click Insert button

Select or enter C:/Windows/Microsoft. NET/framework/v1.1.4322/aspnet_isapi.dll
Cancel the check before "check whether the file exists.
OK

AccessHttp://1234.abc.comIt should be okay.

3. The default homepage is invalid because the ball is directly sent to Asp.net for processing. The default homepage defined by IIS will be invalid. This situation occurs:
AccessHttp://www.abc.comYou cannot access the home page.Http://1234.abc.com/default.aspxAccessible.
To solve this problem, set lookfor/to/default. aspx or index. aspx .. in Web. config to completely solve the problem.

End
-------------------------------

Currently:/Files/rene1018/urlrewriter.rar

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.