【问题标题】:http to https redirect with friendly urls redirecting and rewriting in IIS cuasing loop在 IIS cuasing 循环中使用友好 url 重定向和重写的 http 到 https 重定向
【发布时间】:2018-07-14 22:53:02
【问题描述】:

我有一个已经包含重定向和重写友好 URL 的 webconfig 文件。但是,当我添加 HTTP 到 HTTPS 重定向时,使用这些友好 URL 重定向和重写的 URL 会中断。

Firefox 中的结果...“页面未正确重定向” Chrome 中的结果...“此页面有重定向循环”

谁能告诉我为什么 HTTP 到 HTTPS 重定向会导致这种情况?而且,我怎样才能重新编码这个工作?

这是我的 webconfig 文件...

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <connectionStrings>
      <add name="" connectionString="" />
   </connectionStrings>
   <system.web>
      <compilation targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
      <customErrors mode="Off" />
   </system.web>
   <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
   </appSettings>
   <system.codedom>
      <compilers>
         <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
         <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
      </compilers>
   </system.codedom>
   <system.webServer>
      <urlCompression doStaticCompression="false" doDynamicCompression="false" dynamicCompressionBeforeCache="false" />
      <httpErrors errorMode="Custom">
         <remove statusCode="404" subStatusCode="-1" />
         <error statusCode="404" prefixLanguageFilePath="" path="/errors/404.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      <rewrite>
         <rules>
            <rule name="CanonicalHostNameRule1">
               <match url="(.*)" />
               <conditions>
                  <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
               </conditions>
               <action type="Redirect" url="https://www.example.com/{R:1}" />
            </rule>
            <rule name="Redirect to https" stopProcessing="false">
               <match url="(.*)" />
               <conditions>
                  <add input="{HTTPS}" pattern="^ON$" negate="true" />
               </conditions>
               <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
            </rule>
            <rule name="Redirect-asp-aspx-IIS-new">
               <match url="(.*?)asp$" ignoreCase="true" />
               <action type="Redirect" url="{R:0}x" redirectType="Permanent" />
            </rule>           
            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
               <match url="^viewcategory\.aspx$" />
               <conditions>
                  <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                  <add input="{QUERY_STRING}" pattern="^cat=([^=&amp;]+)$" />
               </conditions>
               <action type="Redirect" url="viewcategory/{C:1}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
               <match url="^viewcategory/([^/]+)/?$" />
               <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               </conditions>
               <action type="Rewrite" url="viewcategory.aspx?cat={R:1}" />
            </rule>
            <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
               <match url="^viewseries\.aspx$" />
               <conditions>
                  <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                  <add input="{QUERY_STRING}" pattern="^cat=([^=&amp;]+)&amp;class2get=([^=&amp;]+)$" />
               </conditions>
               <action type="Redirect" url="viewseries/{C:1}/{C:2}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
               <match url="^viewseries/([^/]+)/([^/]+)/?$" />
               <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               </conditions>
               <action type="Rewrite" url="viewseries.aspx?cat={R:1}&amp;class2get={R:2}" />
            </rule>
            <rule name="RedirectUserFriendlyURL3" stopProcessing="true">
               <match url="^viewproducts\.aspx$" />
               <conditions>
                  <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                  <add input="{QUERY_STRING}" pattern="^series=([^=&amp;]+)$" />
               </conditions>
               <action type="Redirect" url="viewproducts/{C:1}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL3" stopProcessing="true">
               <match url="^viewproducts/([^/]+)/?$" />
               <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               </conditions>
               <action type="Rewrite" url="viewproducts.aspx?series={R:1}" />
            </rule>
         </rules>
         <outboundRules>
            <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
               <match filterByTags="A, Form, Img" pattern="^(.*/)viewcategory\.aspx\?cat=([^=&amp;]+)$" />
               <action type="Rewrite" value="{R:1}viewcategory/{R:2}/" />
            </rule>
            <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
               <match filterByTags="A, Form, Img" pattern="^(.*/)viewseries\.aspx\?cat=([^=&amp;]+)&amp;(?:amp;)?class2get=([^=&amp;]+)$" />
               <action type="Rewrite" value="{R:1}viewseries/{R:2}/{R:3}/" />
            </rule>
            <rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
               <match filterByTags="A, Form, Img" pattern="^(.*/)viewproducts\.aspx\?series=([^=&amp;]+)$" />
               <action type="Rewrite" value="{R:1}viewproducts/{R:2}/" />
            </rule>
            <preConditions>
               <preCondition name="ResponseIsHtml1">
                  <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
               </preCondition>
            </preConditions>
         </outboundRules>
      </rewrite>
      <defaultDocument>
         <files>
            <add value="index.aspx" />
         </files>
      </defaultDocument>
   </system.webServer>
</configuration>

【问题讨论】:

    标签: redirect iis https friendly-url


    【解决方案1】:

    使用这个

           `<rule name="HTTP to HTTPS" enabled="true" patternSyntax="Wildcard" 
            stopProcessing="true">
            <match url="*" negate="false" />
            <conditions logicalGrouping="MatchAny">
            <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
            redirectType="Found" />
            </rule>`
    

    【讨论】:

      猜你喜欢
      • 2018-01-18
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多