ASP.NET Response.Redirect and URL Rewriting

Topics: Developer Forum, User Forum
Apr 15, 2010 at 8:28 PM


I have a web page, and through help here I have a rewrite rule to hide the /folder for various reasons.  So the user requests  When my code does a Response.Redirect("~/default.aspx"), the page redirects to and not like I would like it to be.

Is a redirect rule in the IIRF.ini script helpful here?  Any ideas why the redirect isn't happening?  FYI, the script is in the virtual directory root location, and it does work great when requesting the URL, but the Response.Redirect I cannot figure out what I should request, should I omit the ~/ here?


Apr 16, 2010 at 2:06 AM
Edited Apr 16, 2010 at 2:11 AM

I'm not clear on what's happening, what IIRF rules you have.

I'm also not clear on what you mean by " have a rewrite rule to hide the /folder for various reasons". 

I thought you were rewriting so that /folder was not exposed publicly, but then you said the user requests   So I don't know what's going on at this point.

If you show me the rules you're using, and describe what results you see, I may be able to help.



Apr 16, 2010 at 3:44 AM
Edited Apr 16, 2010 at 3:51 AM

Yeah, I looked up the "~" and it resolves to the root application URL.  The root of your application *is* .  So this is what gets requested, because IIRF doesn't rewrite *outgoing* URLs, only incoming ones.

I think removing the tilde will work (to Response.Redirect("/default.aspx") ), but I'm not absolutely certain.  It ought to be easy enough to test.

Cheeso, I think he could put a RedirectRule at the beginning of his ini rules instead, like this?


RedirectRule ^/folder/(.*) /$1 [R=301]


Seems like a bandaid for faulty application behavior, though.  Of course using IIRF to mask where the application is really residing could be considered a bandaid as well ;-).

Apr 16, 2010 at 7:52 PM

Hey guys,

Thanks for the replies.  I did follow what you sent me the other day, I do this: and took Cheeso's option as is specified here.  So this works great to hide and make it appear as  Perfect.

I even got the form post situation to work correctly; though, in looking at other advice, using Request.RawUrl did not solve my issue because it still has the name of the folder in the RawUrl, and all of the references to the folder, no matter what... so I had to put code in there to remove the folder name...  the folder name is not exactly "folder" as you probably figured out, but is a unique enough name that that won't be a future issue.  So by doing:

if (Request.RawUrl.Contains("/folder"))
     this.Form.Action = Request.RawUrl.Replace("/folder", "");

Worked flawlessly.  I'm so close to getting this solution done.

The only thing left is that when my code does a Response.Redirect("~/default.aspx") or to any other page, the /folder gets back in the URL...  I don't know why...  I assume because of the ~ as @rswitt mentioned, but if its something else, please let me know.  I'll test that and see where that gets me.

And, in case if you are interested, because of a single-sign on implementation, we had to make a structure change to move the application one level down, and don't want to break the links for users who bookmarked the site.  That way, it still works for them, and we can implement the single sign-on flawlessly (which is easiest if each project is a virtual under a root project...


Apr 17, 2010 at 12:39 PM

I agree with rswitt's cmoment.  The tilde is putting /folder in there for you.

You ought to be able to avoid that by redirecting to an absolute URL.