我试图在IIS上获得一个React应用程序重定向到https
的一切。它目前无法正常工作。我可以通过http
和https
访问应用程序和资产,但前者不会重定向到后者。
这是我到目前为止:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Static Assets" stopProcessing="true">
<match url="^(http|https):\/\/([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg))" />
<action type="Rewrite" url="https://{R2}" logRewrittenUrl="true" />
</rule>
<rule name="ReactRouter Routes" stopProcessing="true">
<match url="^(http|https):\/\/(.*)\/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="https://{R2}/index.html" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
根据你的描述,我发现你使用重写而不是重定向。由于重写不会从http重定向到https,您会觉得您的规则不起作用。
我建议你可以尝试使用以下规则。
<rule name="ReactRouter Routes" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="https://www.sample1.com/index.html" logRewrittenUrl="true" />
</rule>
<rule name="Force https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" redirectType="Permanent" />
</rule>