UrlRewriter.dll pseudo-static implementation of two-level domain name Pan-resolution

Source: Internet
Author: User

As you should know, Microsoft's urlrewrite can rewrite URLs, but only the parts after the domain name can be rewritten, not the domain name.

For example, http://http://www.115sou.com/qq/can be rewritten as http://www.115sou.com/show.aspx?id=qq

However, you cannot rewrite http://qq.115sou.com/as HTTP://WWW.115SOU.COM/INDEX.ASPX?ID=QQ.


To achieve this function, the precondition is http://www.115sou.com/is pan-analytic, and then to modify the Urlrewriter.
A total of 2 files to be modified

1.basemodulerewriter.cs

protected virtual void Basemodulerewriter_authorizerequest (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite (app. Request.path, app);
}


Switch

protected virtual void Basemodulerewriter_authorizerequest (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
Rewrite (app. Request.Url.AbsoluteUri, app);
}



is to apply the app. Request.path replaced the 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 to" + Sendtourl);

Rewrite the URL
Rewriterutils.rewriteurl (app. Context, Sendtourl);
Break Exit the For Loop
}
}


Switch

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
}
}


Will

String lookfor = "^" + rewriterutils.resolveurl (app. Context.Request.ApplicationPath, Rules[i]. LookFor) + "$";

Changed it.

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


After completing these 2 changes, recompile the project and copy the resulting DLL to the bin directory.

And then write the rewrite regular in Web. config.

<RewriterRule>
<lookfor>http://(\d+) \.115sou\.com/</lookfor>
<SendTo>/show.aspx?id=$1</SendTo>
</RewriterRule>



Well done, you enter http://shouji.115sou.com/in the IE Address bar, you can see Http://www.115sou.com/index.aspx?id=shouji

The results.

UrlRewriter.dll pseudo-static implementation of two-level domain name Pan-resolution

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.