views:

41

answers:

2

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

A: 

I didnt verify it, but what about refferer?

Ency
Referrer is the referring page, not the requested page. There is a *big* difference.
klausbyskov
Well, if you're redirected to another domain, isnt page where you are redirected from refferer page?
Ency
Url rewriting is not treated like a redirection.
Nicolas Cadilhac
+2  A: 

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

klausbyskov
Thank you, it works. Out of curiosity, is the original domain lost or can I also retrieve it somewhere?
Nicolas Cadilhac
@Nicholas, I don't know of the top of my head, but I'm convinced that it *must* be somewhere in `Params`: http://msdn.microsoft.com/en-us/library/system.web.httprequest.params.aspx
klausbyskov
Its actually in Request.ServerVariables("HTTP_X_REWRITE_URL"), I am sure Request.RawUrl just maps to that. ;-)
CrazyDart
@CrazyDart, yes, and `ServerVariables` are also in `Params` ;-)
klausbyskov
@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?
Nicolas Cadilhac