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.
我有url重写规则,将www.domain2.com重定向到domain1.com根目录下的子文件夹(让我们调用此文件夹子项目)。在我的控制器中,我需要构建一个指向原始未修改路径的URL,但Request.Url属性(如AbsoluteUri或LocalPath)始终包含子项目子文件夹。
In other words, if the user typed:
换句话说,如果用户键入:
www.domain2.com/controller/action
urlrewrite makes it:
urlrewrite使它成为:
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?
我可以硬编码从url中删除子项目并使用domain2开始url但我需要一个通用的代码段,因为这个url重建将在一个可重用的库中。 domain2可能在我的应用程序的设置,但该子文件夹呢?
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 个解决方案
#2
2
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.
我需要这个来缓存localStorage中的一些资源以用于离线Web应用程序。使用asp.net mvc,页面参数被写为页面下的子目录,这会导致相对路径发生变化。因此,我需要绝对路径作为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
#2
2
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.
我需要这个来缓存localStorage中的一些资源以用于离线Web应用程序。使用asp.net mvc,页面参数被写为页面下的子目录,这会导致相对路径发生变化。因此,我需要绝对路径作为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>