微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ReactJS中为嵌套URL重写URL

如何解决在ReactJS中为嵌套URL重写URL

我正在IIS上运行react应用程序,我使用URL重写进行路由配置,这是我的web.config文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <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="^/(docs)" negate="true" />
              </conditions>
              <action type="Rewrite" url="index.html" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

代码适用于localhost / ReactApp / User等网址, 现在我希望它能像localhost / ReactApp / User / Edit一样工作,但不适用于子页面,它显示空白页面, 此问题仅在构建和部署之后出现,请让我知道它如何适用于/ User / Edit嵌套页面或Child页面

解决方法

从子文件夹提供react应用时,您需要在溃败id(x)

中设置基本名称

尝试以下iis网址重写规则:

<BrowserRouter basename='/path/to/subfolder/'>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。