How do I know the source URL after rewriting the url?

I have URL rewriting rules that redirect www.domain2.com to a subfolder under the root of domain1.com (call this subproject of this folder). In my controller, I need to create a URL for the original unmodified path, but the Request.Url properties (for example, AbsoluteUri or LocalPath) always contain a subfolder of the subproject.

In other words, if the user typed:

www.domain2.com/controller/action

urlrewrite does this:

www.domain1.com/subproject/controller/action

and I want to restore the original url:

www.domain2.com/controller/action

I could hard delete the removal of the subproject from url and start the url with domain2, but I need a common piece of code because this url reconstruction will be in the reusable library. domain2 may be in the settings of my application, but what about a subfolder?

The rewrite rule is given as a reference:

<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>

+3
2

.

, localStorage -. asp.net mvc , . localStorage.

, :

        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;

. :

    //  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/

    //  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/

:

, :

// 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>
+2

Source: https://habr.com/ru/post/1770803/


All Articles