2
votes

Summary:

If I have both these things:

  1. ASP.Net page Page1.aspx with Reponse.Redirect("/Page2.aspx") on the Page_Load event
  2. URL rewrite rule on IIS where "*/Page1.aspx" is redirected (303) to "/Page3.aspx"

What runs first, 1 or 2?

(ASP.Net version is 4.0, IIS version is 7.5)


Actual live case

http://inhibitif.com/product/Hair-Free-Deodorant

This route gets mapped to the page Product.aspx where Hair-Free-Face-Serum is the product key. If this page does not find the product key it redirects on the Page_Load event to the full products listing.

http://inhibitif.com/products

That specific key (Hair-Free-Deodorant) has changed (Hair-Free-Deodorant-Key-Lime-Mint-50ml) as per the site owner's request (out of my control). Consequently, any call to that specific product gets redirected to the full product listing (/products). Since, for SEO, it would be better to redirect to the new product page using the new key (as opposed to redirecting to a generic product listing page), I have added this rewrite rule on IIS

<rule name="Old Product Key: Hair-Free-Deodorant" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*/product/Hair-Free-Deodorant" />
    <action type="Redirect" url="/product/Hair-Free-Deodorant-Key-Lime-Mint-50ml" />
</rule>

The rule never triggers. The redirect is always called.

  1. I know I can add this logic to the Page_Load event handler but, shouldn't it be the other way around? Should't the IIS rule get priority?
  2. Perhaps my rule is wrong?

Note: Rewrite rules are working on the site. For example, any call to "www.inhibitif.com" gets rewritten as "inhibitif.com"

2
Thanks for adding that information. I've removed my answer, because I don't think it answers your actual question, now that I see all the details (and it might be actually incorrect). For what it's worth, it looks like the URL rewrite rule in IIS is not being triggered for some reason (either a match is not found, or the Page_Load code just gets called first), since the redirect is 302 (which is what Response.Redirect uses).Josh Darnell
You were correct. The rewrite runs first. The match pattern was malformed. See my own answer to the question. Thanks!cockypup

2 Answers

1
votes

Based on the documention in the "ASP.NET Application Life Cycle Overview for IIS 7.0 " article on MSDN, I would expect the IIS redirect to happen before the Page_Load redirect.

Notice that the stage "Response objects are created for each request." occurs first, which contains these activites:

...application objects such as HttpContext, HttpRequest, and HttpResponse are created and initialized. ...The HttpResponse object contains the response that is sent to the client, which includes all the rendered output and cookies.

At this point, since IIS knows that the response is a 303 redirect to a specific page, I would expect it to load that response into the Response object.

After all that, the "The request is processed by the HttpApplication pipeline" stage occurs, which runs through the ASP.NET Page Life Cycle (including Page_Load).

It would be interesting to actually test this theory out, which I will do when I have a few minutes to do it today. Since Response.Redirect actually uses a 302 redirect, this should be easy to tell by looking in the Response to your request for Page1.aspx (to see if you got a 303 or a 302.

1
votes

Short answer:

The rewrite rule runs first. The pattern was malformed.


Details:

The rewrite rule was malformed. The match pattern did not need the "*/" at the beginning. As indicated on the IIS rules list page, the input needs to match only the "URL path after '/'".

enter image description here

This is the modified (working) rule. Notice I also modified the action url (removed the "\" at the beginning) although this does not seem to be necessary - it works either way. The match edit was the trick:

<rule name="Old Product Key: Hair-Free-Deodorant" patternSyntax="Wildcard" stopProcessing="true">
   <match url="product/Hair-Free-Deodorant" />
   <action type="Redirect" url="product/Hair-Free-Deodorant-Key-Lime-Mint-50ml" />
</rule> 

After changing the rule, the rewrite started triggering first (before the code behind redirect). See details about why it triggers first on jadarnel27's answer below.