Asp. NET to get the original address before the URL rewrite

Source: Internet
Author: User
Tags iis regular expression

In asp.net, if you use a URL rewrite, you get the rewritten address via httpcontext.request. If this address is to be returned to the client (such as redirect), we generally wish to be the friendly address before the rewrite.

The usual use scenario is when we have a page that requires a user to log in to access, we will in the code to determine whether the current access user is logged in, if not logged in, then redirect to the login page, and the current URL through the URL parameter passed to the login page. If you use a URL rewrite, and get the current URL through the Request.Url.AbsoluteUri, the user is logged on after the rewrite of the address, which does not affect the normal use, but from the user experience and the unity of the Url point of view, we would prefer to be rewritten before the address.

Before, we are also in the development of this problem, only as far as possible through JS redirect to the login page (through Location.href to get the current URL) or in the code manually write return address.

Now we've found a workaround where we can find the rewritten URL from the request.headers.

1 if the overriding component is isapi_rewrite, then when accessing the overridden URL, an additional data will be added to the headers: key is X-rewrite-url and the value is the URL before the rewrite.

2 If the overriding component is a URL rewrite module with IIS, the key to the added information in headers is x-original-url.

So we can easily get the URL before the rewrite, the sample code is as follows:

The following is the referenced content:

if (request.headers[ " x-rewrite-url " ] ! = null )
{
Response.Write ( " http:// " + Request.Url.Host + request.headers[ " x-rewrite-url " The ]);
}
else if (request.headers[ "" X-original-url ] ! = null )
{
    Response.Write ( " http:// " + Request.Url.Host + request.headers[ " x-original-url " The }

Digression:

Isapi_rewrite is a small difference from the URL rewrite module of IIS, but it's a lot of trouble migrating from Isapi_rewrite to URL rewrite. For example: For http://www.cnblogs.com/cmt/this URL, isapi_rewrite is used "/CMT/" to match, and the URL rewrite module with "cmt/" to match, the difference between a slash, It causes the migration to modify every regular expression written in Isapi_rewrite. Also, the URL rewrite provides the ability to import rules from Isapi_rewrite without considering this situation.

Related Article

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.