Supports URL rewriting without a suffix

Source: Internet
Author: User
If you want to support directories, you must use iiswriter or other software. In fact, by simply configuring IIS and using urlwriter, you can solve the URL rewriting problem perfectly.

You can change http://abc.domain.com/blog

Turn to http://www.domain.com/xxx.aspx? Username = ABC

Of course, you must first enable the wildcard domain name support of the host.

The procedure is as follows:

A. Open IIS, right-click the site (virtual directory)-> properties-> main directory-> Configuration-> insert-> C: \ WINDOWS \ Microsoft. net \ framework \ v2.0.50727 \ aspnet_isapi.dll. If the file exists, click OK.

B. Modify webconfig:

<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> http: // localhost/(blog) </lookfor>
<Sendto> ~ /Sdfsdf. asxp? Tag = $1 </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

C. Modify urlrewriter
Download source code from Microsoft,

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)
{
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 + "$ ";

OK.

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.