2016-11-05 49 views
0

我試圖強制將所有http流量重定向到https ,同時返回301狀態碼。 所以我在我的Web.Release.config文件中有這個。web.config重定向到https與301狀態代碼

<rewrite> 
    <rules> 
    <rule name="Redirect to https" xdt:Transform="Insert"> 
     <match url="(.*)"/> 
     <conditions> 
     <add input="{HTTPS}" pattern="Off"/> 
     <add input="{REQUEST_METHOD}" pattern="^get$|^head$" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" statusCode="301" redirectType="Permanent" /> 
    </rule> 
    </rules> 
</rewrite> 

,我不斷收到307而不是301。我如何強制它返回301狀態碼?

回答

1

<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" statusCode="301" redirectType="Permanent" />

根據你的代碼,我們可以找到您指定redirectType="Permanent",它應該返回301狀態碼。我試圖在我身邊重現這個問題,這裏是我的配置文件。

的Web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433--> 
<configuration> 
<system.web> 
    <compilation debug="true" targetFramework="4.5"/>  <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <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> 
</configuration> 

Web.Release.config

<?xml version="1.0" encoding="utf-8"?> 

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> 

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <!--In the example below, the "SetAttributes" transform will change the value of "connectionString" to use "ReleaseSQLServer" only when the "Match" locator finds an attribute "name" that has a value of "MyDB". 

    <connectionStrings> 
     <add name="MyDB" connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 
    </connectionStrings>--> 
    <system.web> 
    <compilation xdt:Transform="RemoveAttributes(debug)" /> 
    <!--In the example below, the "Replace" transform will replace the entire 
     <customErrors> section of your web.config file. 
     Note that because there is only one customErrors section under the 
     <system.web> node, there is no need to use the "xdt:Locator" attribute. 

     <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly" xdt:Transform="Replace"> 
     <error statusCode="500" redirect="InternalError.htm"/> 
     </customErrors>--> 
    </system.web> 
    <system.webServer xdt:Transform="Insert"> 
    <rewrite> 
     <rules> 
     <rule name="Redirect to https"> 
      <match url="(.*)"/> 
      <conditions> 
      <add input="{HTTPS}" pattern="Off"/> 
      <add input="{REQUEST_METHOD}" pattern="^get$|^head$" /> 
      </conditions> 
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
     </rule> 
     </rules> 
    </rewrite> 
    </system.webServer> 
</configuration> 

我可以看到網絡工具301狀態碼,當我訪問我的網站。

enter image description here

我不斷收到307而不是301我怎樣才能迫使它返回301狀態碼?

您可以檢查您的Web.config文件並確定是否定義了其他URL更改規則來更改狀態代碼。

+0

任何想法從哪裏開始尋找任何超過這個寫的東西?我沒有其他的重定向規則 –

+0

夠奇怪,只有chrome返回307。FF和邊緣顯示301重定向.. –