asp.net - 如何在 IIS 中将所有非 www URL 重定向到 https ://www.?

标签 asp.net redirect iis url-rewriting url-rewrite-module

我想在 IIS 8.5 中添加正确的 301 永久重定向规则。我添加了以下规则,但它不起作用。

 <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^(http:\/\/){0,1}(www\.){0,1}([^\.]+)\.([^\.]+)$" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
        </rule>

条件
  • 如果 URL 在 HTTPS 中并且包含“www”。然后没有重定向。示例:https://www.example.com
  • 如果 URL 是 HTTP,那么它应该被重定向到 HTTPS。示例:http://www.example.com应该重定向到 https://www.example.com
  • 如果 URL 使用 HTTPS 但不包含“www”。那么它应该被重定向到带有“www”的HTTPS站点。字首。示例:https://example.com应该重定向到 https://www.example.com
  • 如果 URL 既不包含 HTTPS 也不包含 WWW,则使用“www”重定向到 HTTPS URL。字首。示例:http://example.com应该重定向到 https://www.example.com

  • 总而言之,每个 URL 都应该是 HTTPS 并且应该有“www”。字首。

    注意:我已安装 URL Rewrite Module在 IIS 中。

    任何人都可以帮助我实现这一目标吗?

    最佳答案

    我通过在 Web.config 中添加两个 URL 重写规则来管理它文件:

  • 用于将非 www 重定向到 https://www .{domain}.com/...
  • 将 HTTP 重定向到 HTTPS
    <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
          </conditions>
          <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
          <serverVariables>
            <set name="Redirect" value="false" />
          </serverVariables>
     </rule>
    
    
    <rule name="Force HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
     </rule>
    
  • negate="true"的所有条件用于排除。因此,所有包含“localhost”、“stage”和“dev”的 URL 都被排除在 URL 重写之外。如果不需要,您可以删除这些条件。

    阅读更多关于 否定 属性在 http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    关于asp.net - 如何在 IIS 中将所有非 www URL 重定向到 https ://www.?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406039/

    相关文章:

    asp.net - 在ASP.NET + WCF下长时间运行的线程进程

    c# - 通过从网页将字符串查询传递到通用处理程序来避免空值 - asp.net

    php - Laravel:如何在 Controller 方法执行后重定向

    asp.net - IE6安全登录(通过VirtualPC调试)

    .net - 如何修复 "The Development environment shouldn' 无法为已部署的应用程序启用”?

    iis - IIS 中的 session 超时和空闲超时有什么区别?

    c# - 为什么这个 linq 表达式不起作用?

    css - 响应式 Bootstrap 设计导致 div 通过添加 help-block 移动

    redirect - 对 Web 应用程序中的无限重定向进行故障排除

    http - 为什么 http ://google. com/a/bogus/url 不重定向到 404 URL,为什么这是首选?