带有React Router v4的IIS规则 [英] IIS Rules with React Router v4

查看:73
本文介绍了带有React Router v4的IIS规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BrowserRouter使路由与IIS和React Router v4一起使用.

I'm trying to get my routes to work with IIS and React Router v4 using BrowserRouter.

在我的本地主机上,我的所有路由均按预期工作. React Router可以像我希望的那样处理所有事情:

On my localhost I have all my routes working as expected. React Router handles everything like I want it to:

  • http://www.example.com/appfolder/dest
  • http://www.example.com/appfolder/dest/CODE
  • http://www.example.com/appfolder/dest/CODE/INVALID/URL

现在,在IIS中,我设置了一个忽略'/appfolder/api/*'的规则,目的是为我的应用程序提供api.那行得通.

Now, in IIS I've set up a rule that ignores '/appfolder/api/*' for the purpose of serving an api for my app. That works.

如何使IIS重定向到' http://www.example.com/appfolder/dest/CODE ",并让React Router根据其规则对其进行处理.如果我重定向到'/appfolder/index.html',我会放开我想保留以供ReactRouter处理的'CODE'.

How do I get IIS to redirect to 'http://www.example.com/appfolder/dest/CODE' and have React Router handle it according to its rules. If I redirect to '/appfolder/index.html' I loose my 'CODE' which I'd like to keep for ReactRouter to handle.

如果我尝试重定向到" http://www.example.com/appfolder/dest/CODE "(通过使用正则表达式捕获组),我得到的是"ERR_TOO_MANY_REDIRECTS".

If I try to redirect to 'http://www.example.com/appfolder/dest/CODE' by using regexp capture groups, I get a 'ERR_TOO_MANY_REDIRECTS'.

我当前在我的web.config中有此规则:

I currently have this rule in my web.config:

    <rule name="ReactRouter Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/appfolder/api/" negate="true" />
      </conditions>
      <action type="Redirect" url="/appfolder/dest/CODE" />
      <!-- also tried this -->
      <!-- action type="Redirect" url="/appfolder/index.html" /-->
    </rule>

推荐答案

我找到了解决方案.我将动作类型更改为重写",它就像一种魅力.

I found the solution. I changed my action type to Rewrite and it worked like a charm.

IIS规则最终解决方案:

IIS rule final solution:

<rule name="ReactRouter Routes" stopProcessing="true">
  <match url=".*" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_URI}" pattern="^/appfolder/api/" negate="true" />
  </conditions>
  <action type="Rewrite" url="/appfolder/index.html" />
</rule>

这篇关于带有React Router v4的IIS规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆