IIS上React的Http到https重定向

332nm8kg  于 2022-11-12  发布在  React
关注(0)|答案(2)|浏览(209)

我正在尝试让IIS上的React应用重定向到https的所有内容。目前无法工作。我可以通过httphttps访问应用和资产,但前者不会重定向到后者。
这是我开始的内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Static Assets" stopProcessing="true">
          <match url="([\S]+[.](html|htm|svg|json|js|css|png|gif|jpg|jpeg|map))" />
          <action type="Rewrite" url="/{R:1}" />
        </rule>
        <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" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

以下是我目前所掌握的情况:

<?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>
eagi6jfj

eagi6jfj1#

我把这个放在这里,希望它能帮助到别人。我花了几个小时才找到一个解决方案。使用web.config下面的代码,我可以重写index.html并重定向到HTTPS。这也适用于更深的URL。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTPS Redirect" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
                </rule>
                <rule name="ReactRouter Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.html" />
                </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>
8cdiaqws

8cdiaqws2#

根据你的描述,我发现你使用了rewrite而不是重定向。由于rewrtie不会从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>

相关问题