url在web.config http中重写为https,非www重写为www并且没有尾随斜杠

时间:2018-01-24 13:46:27

标签: ssl url-rewriting web-config

Web.config URL重写令我头疼:-) 我试图在这些条件下设置重写规则:

  1. 它应该只生成一个重定向
  2. 忽略资源
  3. 删除default.aspx
  4. 删除尾部斜杠
  5. 不使用自定义文件扩展名时,
  6. 小写(不是查询字符串)
  7. 强制www
  8. 强制https
  9. 所有这一切都很有效,除非原始网址已经包含WWW。如果网址有WWW,则会遵循所有规则,但不会重定向到HTTPS。

    任何帮助都会受到极大的欢迎!

    这是我到目前为止(inspired by this great article):

      <rules>
    
    
        <rule name="WhiteList - resources" stopProcessing="true">
          <match url="^resources/" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="None" />
        </rule>
    
        <rule name="SEO - remove default.aspx" stopProcessing="false">
          <match url="(.*?)/?default\.aspx$" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
          </conditions>
          <action type="Rewrite" url="_{R:1}" />
        </rule>
    
        <rule name="SEO - Remove trailing slash" stopProcessing="false">
          <match url="(.+)/$" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="_{R:1}" />
        </rule>
    
        <rule name="SEO - ToLower" stopProcessing="false">
          <match url="(.*)" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_URI}" pattern="\.pngx|\.jpgx|\.gifx|\.bmpx|\.orcx" negate="true" />
            <add input="{HTTP_METHOD}" pattern="GET" />
            <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
          </conditions>
          <action type="Rewrite" url="_{ToLower:{R:1}}" />
        </rule>
    
        <rule name="ADD WWW IF NOT PRESENT" stopProcessing="true">
          <match url="^(_*)(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^www\.testssl\.orca-bree\.be$" negate="true" />
            <add input="{HTTP_METHOD}" pattern="GET" />
          </conditions>
          <action type="Redirect" url="https://www.testssl.orca-bree.be/{R:2}" redirectType="Permanent" />
        </rule>
    
        <rule name="redirect" stopProcessing="true">
          <match url="^(_+)(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
          </conditions>
          <action type="Redirect" url="{R:2}" redirectType="Permanent" />
        </rule>
    
      </rules>
    

1 个答案:

答案 0 :(得分:0)

我没有测试它,但它应该有效:

<rule name="Redirect to www" patternSyntax="ECMAScript" stopProcessing="true">
  <match url=".*"></match>
  <conditions logicalGrouping="MatchAny">
    <add input="{HTTP_HOST}" pattern="^mydomain.be$"></add>
    <add input="{HTTPS}" pattern="off"></add>
  </conditions>
  <action type="Redirect" url="https://www.mydomain.be/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
<rule name="Remove trailing slash" stopProcessing="true">
  <match url="(.*)/$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Redirect" redirectType="Permanent" url="https://www.mydomain.be/{R:1}" />
</rule>

我改变了什么:

  • 您的规则的行动。现在,它们包含wwwhttps
  • 的完整域名
  • 在第一条规则
  • 中为您的条件添加了MatchAny