Use URL rewrite if the original URL contains querystring

Source: Internet
Author: User

I have rewritten my website.Program, In the use of msdn
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnaspp/html/urlrewriting. asp
URL rewrite Method

Today, I found that the record of the original access address is still in the search engine, and many of my friends access the address through it.
Of course, I don't want to consider that all these connections are directed to the error page, so I want to add a rule in the URL rewirte.
<Lookfor> ~ /Showarticle. aspx \? Id = (\ D +)
<Sendto> is ~ /Show. aspx \? Id = (\ D +)
When I access this page, Error 404 is reported.
Take a closer look at the url writeCode
Last found

Protected virtual void rewritermodule_authorizerequest (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
Rewrite (App. Request. Path, APP); // here, request. Path is used.
}

Tested by me
For an address http: // localhost/test/requestpath. aspx/SSS. aspx? Id = 2222 access
The result is:

Request. Path:/test/requestpath. aspx/SSS. aspx
Request. url. tostring (): http: // localhost/test/requestpath. aspx/SSS. aspx? Id = 2222
Request. pathinfo:/SSS. aspx
Request. rawurl:/test/requestpath. aspx/SSS. aspx? Id = 2222

I changed the above Code

Protected virtual void rewritermodule_authorizerequest (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
Rewrite (App. Request. rawurl, APP );
}

When I run my program, I find that my program reports an error, prompting that my parameters are incorrect,
After tracking, we found that querystring passed two ID parameters.
After tracking code, we found that internal static void rewriteurl (httpcontext context, string sendtourl, out string sendtourllessqstring, out string filepath)

If (context. Request. querystring. Count> 0)
{
If (sendtourl. indexof ('? ')! =-1)
{
Sendtourl + = "&" + context. Request. querystring. tostring ();
}
Else
{
Sendtourl + = "? "+ Context. Request. querystring. tostring ();
}
}
Here, context. Request. querystring is added to sendtourl.

If this part is removed, all pages without the querystring parameter cannot be passed correctly.
At last, only when rewrite. config is available

<! --
If lookfor contains querystring, you do not need to write querystring in sendto,
Because the context. Request. querystring of the context content has been passed
-->
<Rewriterrule>
<Lookfor> ~ /Showarticle. aspx \? Id = (\ D +) </lookfor>
<Sendto> <! [CDATA [~ /Show. aspx]> </sendto> <! -- Here -->
</Rewriterrule>

Everything is okay. This address can be accessed.
Http://www.aspxboy.com/ShowArticle.Aspx? Id = 214
:-)
 

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.