Implement level-2 or multi-level domain names in ASP. NET (modify urlrewrite)

Source: Internet
Author: User
You should know that Microsoft's urlrewrite can rewrite the URL, but it can only rewrite the part after the domain name, rather than the domain name. For example, you can rewrite http: // http://www.abc.com//1234/ override to http://www.abc.com/show.aspx? Id = 1234, but cannot
Http://1234.abc.com/override to http://www.abc.com/show.aspx? Id = 1234.

To achieve this function, the premise is http://www.abc.com/is Pan resolution, and then to modify urlrewriter.
2 files to be modified in total

1. basemodulerewriter. CS

Protected   Virtual   Void Basemodulerewriter_authorizerequest ( Object Sender, eventargs E)
{
Httpapplication app
=(Httpapplication) sender;
Rewrite (App. Request. Path, APP );
}

Change

Protected   Virtual   Void Basemodulerewriter_authorizerequest ( Object Sender, eventargs E)
{< br> 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 =   " ^ "   + Rewriterutils. resolveurl (App. Context. Request. applicationpath, 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 "   + Sendtourl );

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

}

Change

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 "   + 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

String lookfor = "^" + rules [I]. lookfor + "$ ";

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

Then, rewrite the Regular Expression in Web. config.

< Rewriterrule >
< Lookfor > Http: // (\ D +) \. ABC \. com/ </ Lookfor >
< Sendto > /Show. aspx? Id = $1 </ Sendto >
</ Rewriterrule >

Now, you can enter http://1234.abc.com/in the IE address bar to see http://www.abc.com/show.aspx? Id = 1234

The result is

If you encounter problems in practical applications, please referArticle"Modify urlrewrite to rewrite the domain name", you need to pay attention to the problem, hope to help you!

Appendix:

About urlrewriter

Http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

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.