Pseudo-static and modify urlrewrite to overwrite the Domain Name

Source: Internet
Author: User
Everyone should know that Microsoft's urlrewrite can rewrite the URL, but also can only rewrite the part of the domain name after, rather than rewrite the domain name, such as: can rewrite the http://www.abc.com/1234/ 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 implement this function, the precondition is that www.abc.com is pan-parsed, and then urlrewriter needs to be modified.
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)
{
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 to" + 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 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

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>

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

The result is

Appendix:

About urlrewriter

Http://www.microsoft.com/china/msdn/library/webservices/asp.net/Urlrewriting. MspxUrlrewriterUseExperience Summary
Urlrewriter is a URL rewriting component encapsulated by Microsoft.UseIt saves me a lot of time for self-development.
Well, I started to talk about my application experience. This is just a cainiao experience, so you don't have to read it.

Step 1: download this component. Decompress the package and copy urlrewriter. DLL to your project bin directory.

Step 2: Add the following content to Web. config:

<? XML version = "1.0" encoding = "gb2312"?>
<Configuration>
<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>

Step 2: add the rewritten rule node:
For example:
<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /Examples/(. [0-9] *)/. html </lookfor>
<Sendto> ~ /Search/search_sell.aspx? Id = $1 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Logs/search_logs/. aspx </lookfor>
<Sendto> ~ /Search/search_sell.aspx </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Buy/(. [0-9] *)/. html </lookfor>
<Sendto> ~ /Search/search_buy.aspx? Id = $1 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Buys/(. [0-9] *)/. html </lookfor>
<Sendto> ~ /Buys/show. aspx? Id = $1 </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>

This depends on your needs. If you are not familiar with regular expressions, there is no way to find the regular expression or use your high IQ to find the regular expression. You can use it with a slight change. Haha. If you cannot figure it out, Google the regular expression. (I started to guess by referring to other people's configurations. I used it right, huh, huh. Later I read the relevant information and found it useful .)

Step 3: add the module configuration (written in <system. Web> ):
For example:
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
(HTTPProgramTo process the rewrite)

Now we can try it.

So the input: http: // 127.0.0.1: 8080/Hangzhou/1. aspx appears. However, if it is changed to: http: // 127.0.0.1: 8080/Hangzhou/1.html
Dizzy. Khan...
The reason is that we didn't use Asp.net's ISAPI to parse HTML parsing.
The solution is...

Step 4: add an extension item with the same configuration as the ASPX page in IIS/your site/properties/home directory/configuration/. Note: Do not select "check whether the file exists". Otherwise, the file cannot be found.

Try again now. What? #¥ % # ¥ %. Haha. Don't worry. Let's take a look at it. We didn't configure. html in Web. config and used this module for parsing.

Step 5: add the following to the module configuration:
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
<Add verb = "*" Path = "*. html" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>

It's okay now, huh, huh. I am so excited. Don't worry, it's just the simplest. If your page has a return request. For example, if you put the DataGrid on the page and there are pages, you will find that the page goes down to the next page, and then you will find a problem again.
What should I do now? In fact, I have mentioned it on Microsoft's website. I will briefly describe it here.

Step 6: add the component for form return Persistence:
Find actionlessform. dll in the project you downloaded and put it in the bin directory of your project.

Then add the following to your page:
<% @ Register tagprefix = "SKM" namespace = "actionlessform" assembly = "actionlessform" %>
Change <form...>:
<SKM: Form ID = "Your form name" method = "Post" runat = "server">
.....
</SKM: Form>

That's all. Now you can rest assured. Everything is as you wish.

From previousArticle, We can rewrite the part after the domain name, so can we rewrite the previous part to implement the second-level domain name?
The answer is yes.
In this way, we need to modify urlrewriter first. For details about how to modify it, see jiangda fish's blog.

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

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

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

After the modification, we copy the urlrewriter. DLL to the bin directory of our project. Is this the end? No.
First, make sure that your project has previously configured urlrewriter as I wrote in the previous article. Otherwise, let's look back at the article.

If your project has been configured, we need to do the following:

1. Make sure that your domain name supports wildcard resolution. Then your website is the default website, otherwise it will not be implemented (at least I have not found a good solution)
2. In IIS configuration: in IIS/your site/properties/home directory/configuration/ Insert a new in the configuration of the wildcard application. Set the executable file to the same configuration as the above ASPX page (Be sure not to check "check whether the file exists "). (It is used to process all requests through the ISAPI of Asp.net. Only in this way can we rewrite all the addresses .)
3. Check your website host header. The first host header value must be blank; otherwise, an error occurs. You can add the domain name as needed to see how many domain names you want to bind. (This method was developed by Jiang Dayu. We have tried a lot for this error. Thank you for your appreciation ...)
4. Finally, rewrite your web. config file.
As mentioned in the previous section
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
<Add verb = "*" Path = "*. html" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
Changed:
<Httpmodules>
<Add type = "urlrewriter. modulerewriter, urlrewriter" name = "modulerewriter"/>
</Httpmodules>
(Rewrite with the HTTP module instead of the HTTP program. Otherwise, the address must be overwritten .)
Then we can modify our rewrite RegEx:
<Rewriterrule>
<Lookfor> http: // (. [0-9] *)/. 178b2b/. com/</lookfor>
<Sendto> ~ /Search/search_sell.aspx? Id = $1 </sendto>
</Rewriterrule>
Well, now you can search for the appropriate category by entering the http://1.178b2b.com. But if you are smart, you will immediately find out. Dizzy, the home page cannot enter. Haha. Of course. You have to add a rewrite regular expression to the homepage.
<Rewriterrule>
<Lookfor> http: // www/. 178b2b/. com/</lookfor>
<Sendto> ~ /Index.htm </sendto>
</Rewriterrule>

Success. It feels so bad. Haha. Don't worry. If the second-level domain name points to the pictures connected to other pages with relative addresses under the directory, you have to be busy, you have to change all to the following method:
<A href = http://www.178b2b.com/cxlm/league.html target = "_ blank"> Integrity Alliance </a>

the preceding method uses urlrewriter to implement second-level domain names. I hope everything goes smoothly.

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.