在我的IIS上,我按以下方式配置了重写规则。 有一个路由器站点侦听端口80并通过指定的资源重写到适当的网站,例如当用户输入主机http://testpage.com/current时,它将其重写到托管在端口5001下的另一个网站。示例配置:
<rule name="RewriteRule" stopProcessing="true">
<match url="^current(.*)?" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="http://{HTTP_HOST}:5001/{R:1}" />
</rule>
在5001下托管的网站包含index.html和javascript文件index.js
,在index.html
中引用了这样的内容:
<script type="text/javascript" src="/index.js">
当我替换
时,整个配置工作得非常好 <match url="^current(.*)?" />
带
<match url="^(.*)?" />
但是,当我在此处使用current
时,它会在位于端口5001下托管的页面中找到index.html
,但却无法找到index.js
。我怀疑原因是因为它试图找到/current/index.js
但它并不存在。
我一直认为我的RewriteRule
基本上应该重写网址
http://testpage.com:5001
然后获取index.html
并从当前目录中解析index.js
,这样它就不应该了解&#34;当前&#34;资源。
有没有一种简单的方法来解决这个问题?
显然,当我进入这样的网站:http://testpage.com:5001
时,绕过重写规则就行了。
答案 0 :(得分:0)
我将回答这个问题以供将来参考。这不是重写规则的问题。问题在于html解析资源的方式。 我们需要做几个步骤:
设置<base href="virtualpath">
,在此示例中,虚拟路径应等于/current
。我们在TeamCity部署期间使用webpack在此处输入正确的值。例如:
<script type="text/javascript">
window.virtualpath = window.location.pathname.toLowerCase().indexOf("${virtual_path_legacy}") >= 0 ? "/${virtual_path_legacy}" : "";
document.write("<base href='" + window.virtualpath + "/'/>");
</script>
new webpack.DefinePlugin({ "process.env": { NODE_ENV: JSON.stringify("production") }, virtual_path_legacy: JSON.stringify(virtual_path_legacy) })
在webpack.config.js
中确保您的publicPath
是&#34;&#34;而不是&#34; /&#34;
如果您使用SignalR
,请确保将虚拟路径传递给$.hubConnection
(如果SignalR托管在虚拟路径下)
在.NET Core中:
app.UseAppBuilder(appBuilder => appBuilder.MapSignalR(rootPath, hubConfiguration), camelCaseSerialization);
如果您使用react-router
,请确保将虚拟路径传递给useRouterHistory
。例如:
const browserHistory = useRouterHistory(createHistory)({
basename: infoService.virtualpath,
});
fetch
之类的库连接到API,请确保为所有api url添加虚拟路径前缀(如果您的API也使用虚拟路径)