10

I have url rewrite rules which redirect www.domain2.com to a subfolder under the root of a domain1.com (let's call this folder subproject). In my controller, I need to construct a URL to the original non modified path but the Request.Url properties (like AbsoluteUri or LocalPath) always contain the subproject subfolder.

In other words, if the user typed:

www.domain2.com/controller/action

urlrewrite makes it:

www.domain1.com/subproject/controller/action

and I wish to reconstruct the original url:

www.domain2.com/controller/action

I could hardcode the removal of subproject from the url and begin the url with domain2 but I need a generic piece of code because this url reconstruction will be in a reusable library. domain2 could be in the settings of my app but what about the subfolder?

In reference, here is the rewrite rule:

<rewrite>
    <rules>
        <rule name="Redirect to subproject">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(www.)?domain2.com" />
                <add input="{PATH_INFO}" pattern="^/subproject/" negate="true" />
            </conditions>
            <action type="Rewrite" url="\subproject\{R:0}" />
        </rule>
    </rules>
</rewrite>

Thank you

2 Answers 2

28

You should be able to find it in Request.RawUrl.

6
  • Thank you, it works. Out of curiosity, is the original domain lost or can I also retrieve it somewhere? Commented Oct 21, 2010 at 20:43
  • @Nicholas, I don't know of the top of my head, but I'm convinced that it must be somewhere in Params: msdn.microsoft.com/en-us/library/… Commented Oct 21, 2010 at 20:47
  • Its actually in Request.ServerVariables("HTTP_X_REWRITE_URL"), I am sure Request.RawUrl just maps to that. ;-)
    – CrazyDart
    Commented Oct 21, 2010 at 22:05
  • @CrazyDart, yes, and ServerVariables are also in Params ;-) Commented Oct 21, 2010 at 22:19
  • 3
    @CrazyDart, after some googling, it seems HTTP_X_REWRITE_URL is non standard (I tried locally and it returns empty string). Using Reflector, I see that RawUrl looks into the CACHE_URL server variable (locally it works well and contains the original domain, but it's not in Params). Is it safe to use this one? Commented Oct 21, 2010 at 23:46
4

Here is a good way to get the current directory even when using rewrite.

I needed this to cache some resources in localStorage for an offline web app. With asp.net mvc, the page parameters are written as subdirecties under a page which causes the relative paths to change. Therefore, I needed the absolute path as a key for the resource in the localStorage.

Anyway, here it is:

        var applicationRoot = new System.Uri(Request.Url, Href("~"));
        var rawURl = Request.RawUrl;
        var rawUrlAbsolute = new System.Uri(serverRoot, rawURl);
        var relativeToCurrentPath = Href("SOMETHING_DOESNT_MATTER/../");
        var clientPathAbsolute = new System.Uri(rawUrlAbsolute, relativeToCurrentPath);
        var clientPathAbsoulteStr = clientPathAbsolute.AbsoluteUri;

This took me many attempts to figure out. Here is an example using my website:

The path without rewriter

    //  applicationRoot         = http://www.toldpro.com/
    //  rawURl                  = /Apps/GreekBible/App/A/B/C
    //  rawUrlAbsolute          = http://www.toldpro.com/Apps/GreekBible/App/A/B/C 
    //  relativeToCurrentPath   = ../../../
    //  clientPathAbsolute      = http://www.toldpro.com/Apps/GreekBible/
    //  clientPathAbsoulteStr   = http://www.toldpro.com/Apps/GreekBible/

The path rewriten to a subdomain

    //  applicationRoot         = http://greekbible.toldpro.com/Apps/GreekBible/
    //  rawURl                  = /App/A/B/C
    //  rawUrlAbsolute          = http://greekbible.toldpro.com/App/A/B/C 
    //  relativeToCurrentPath   = ../../../Apps/GreekBible/
    //  clientPathAbsolute      = http://greekbible.toldpro.com/Apps/GreekBible/
    //  clientPathAbsoulteStr   = http://greekbible.toldpro.com/Apps/GreekBible/

The rewrite rule:

For more information about this, the following is the rewrite rule I used:

// Rewrite: 
// http://greekbible.toldpro.com/App/* (Client)
// to
// http://www.toldpro.com/Apps/GreekBible/App/* (Actual)

    <rule name="Subdomain rewriter" stopProcessing="true">
      <match url="App/(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.toldpro\.com$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^greekbible\.toldpro\.com$" />
      </conditions>
      <action type="Rewrite" url="/Apps/GreekBible{URL}" />
    </rule>
1
  • This took me many hours to solve, so I thought someone could use it.
    – Rick Love
    Commented Sep 19, 2012 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.