我对“React 路由”和“IIS 配置”有疑问。 我在IIS中添加了以下url重写作用:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
使用该配置,当我使用应用程序导航时,它工作正常,但如果我尝试刷新页面,它会变成空白,看来 url 重写不起作用。 (如果我尝试使用节点服务器,则不会发生这种情况)
最佳答案
看起来你的url重写规则配置是正确的,我认为问题应该是React路由没有处理url,如果索引页没有将请求发送到React路由,这就会导致问题。
在这种情况下,您可以尝试添加到 Index.html 页面,这应该可以解决您的问题。并且 href
属性应根据您的 React 应用程序进行调整。
您也可以引用这个类似案例:How do I setup react-router clean URL's with IIS?
关于reactjs - React 和 IIS 重写 url 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71423579/