ssl - 在 web.config 中将 URL 重写为 https 并且非 www 到 www 并且没有尾部斜杠

标签 ssl url-rewriting web-config

Web.config URL 重写让我很头疼:-) 我正在尝试根据这些条件设置重写规则:

  1. 它应该只产生一个重定向
  2. 忽略资源
  3. 删除 default.aspx
  4. 删除尾部斜杠
  5. 不使用自定义文件扩展名时小写(不是查询字符串)
  6. 强制 www
  7. 强制https

所有这些都很好用,除非原始 url 已经包含 WWW。如果 url 有 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>

最佳答案

我没有测试它,但它应该可以工作:

<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

关于ssl - 在 web.config 中将 URL 重写为 https 并且非 www 到 www 并且没有尾部斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48424028/

相关文章:

Laravel cName SSL 证书

linux - apache2 重写规则不起作用

c# - HTTP 错误 414。请求 URL 太长。网站

c# - 如何通过 web.config 重定向 ASP.NET

ruby - Watir-Webdriver、PhantomJS 和重定向到 https 的 URL

Python 请求 SSL 主机名不匹配错误

ssl - 让 Jmeter 使用来自 keystore 的不同证书来发出请求

Tomcat 6 URL Rewrite 将所有出现的字符转换为不同的字符

apache - 在 PHP 中从 .htaccess 重写 URL 获取查询字符串参数

sql-server - 为什么连接字符串在生产 asp.net 中不起作用?