URL rewriting implements any second-level domain name or multi-level domain name (fixed the problem of parameter interruption)

Source: Internet
Author: User

Brief Review:
Modifying Microsoft's urlrewrite can rewrite the URL, where the domain name needs to 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-resolved, and added a Host Header empty ing in 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. I also encountered this problem with Aspx. You need to add a "/" in the rewrite rule to the rule at the end of lookfor and then change it:
<Rewriterrule>
<Lookfor> http: // (\ D +) \. ABC \. com/</lookfor>
<Sendto>/show. aspx? Id = $1 </sendto>
</Rewriterrule>
Another problem is that the modified rewrite is unfriendly to the parameter, the URL after rewriting if there is a parameter such as http://www.abc.com/news.html? Id = 1000. If it is overwritten, 404 is returned. The solution to parameter interruption after rewriting is provided below:
Method 1:
Modify the rewrite rule to make the regular expression accept parameters:
<Rewriterrule>
<Lookfor> http: // (\ D +) \. ABC \. com/news.html (. {0,}) </lookfor>
<Sendto>/show. aspx? Id = $1 </sendto>
</Rewriterrule> all your parameters can match the news page, such as ID and paging, so that the parameters can be used 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 ,}) $ ";

End
-------------------------------
My Project Website: http://www.at0086.com/has a lot of second-level domain names such as: hotel.at0086.com, rewrite examples of www.at0086.com/tsinghuau/

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zj1103/archive/2008/08/30/2851381.aspx

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.