【问题标题】:Http to https redirect for React on IISIIS 上 React 的 Http 到 https 重定向
【发布时间】:2019-08-17 11:26:53
【问题描述】:

我正在尝试让 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>

【问题讨论】:

    标签: reactjs redirect iis url-rewriting react-router


    【解决方案1】:

    根据您的描述,我发现您使用重写而不是重定向。由于 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>
    

    【讨论】:

    • 谢谢,我以前尝试过这样的强制重定向。不幸的是,它没有重定向更深的 URL,例如http://example.com/users。它仅适用于 http://example.com 重定向到 https://example.com
    • 您能分享一下您当前的 url 重写规则吗?您的匹配网址模式似乎有问题。
    • 除了将https://www.sample1.com/index.html 更改为https://{HTTP_HOST}/index.html 之外,我几乎完全按照您在此处所写的进行了尝试。它给出了 500 错误,
    猜你喜欢
    • 2013-05-02
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2018-01-18
    相关资源
    最近更新 更多