如何将所有非www URL重定向到https:// www。在IIS?

编程入门 行业动态 更新时间:2024-10-28 12:19:24
本文介绍了如何将所有非www URL重定向到https:// www。在IIS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to add the proper 301 permanent redirect rule in IIS 8.5. I've added the following rules but it is not working.

<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="www.{C:3}.{C:4}" redirectType="Permanent" /> </rule>

条件

  • 如果URL是HTTPS并且包含www。然后没有重定向。示例: www.example
  • 如果URL在HTTP中,然后应将其重定向到HTTPS。示例: www.example 应重定向到 www.example
  • 如果网址是HTTPS,但不包含www。然后应该使用www将其重定向到HTTPS站点。字首。示例: example 应重定向到 www.example
  • 如果网址既不包含HTTPS也不包含WWW,则使用www重定向到HTTPS网址。字首。示例: example 应重定向到 www.example
  • If the URL is in HTTPS and contains "www." then NO redirect. Example: www.example
  • If the URL is in HTTP then it should be redirected to HTTPS. Example: www.example should be redirected to www.example
  • If the URL is in HTTPS but does NOT contain "www." then it should be redirected to HTTPS site with "www." prefix. Example: example should be redirected to www.example
  • If the URL neither contains HTTPS nor WWW then redirect to HTTPS URL with "www." prefix. Example: example should be redirected to www.example

总结一下,每一个网址应为HTTPS,并且应为www。前缀。

注意:我已安装 URL重写模块。

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

Can anyone please help me to achieve this?

推荐答案

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

I managed it by adding two URL rewrite rules in Web.config file:

  • 用于将非www重定向到 https:// www 。{ domain} /...
  • 将HTTP重定向到HTTPS

  • For redirecting non-www to www.{domain}/...
  • Redirecting HTTP to 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="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="{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule>

  • 所有条件 negate =true用于排除。因此,包含localhost,stage和dev的所有URL都将从URL重写中排除。如果不需要,您可以删除这些条件。

    All the conditions with negate="true" are used for exclusion. Hence all URLs which contains "localhost", "stage", and "dev" are excluded from URL rewrite. You can remove these conditions if not required.

    在否定属性的信息-module / creating-rewrite-rules-for-url-rewrite-modulerel =nofollow> www.iis/learn/extensions/url-rewrite-module/creating-rewrite-rules -for-the-url-rewrite-module

    Read more about negate attribute at www.iis/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    更多推荐

    如何将所有非www URL重定向到https:// www。在IIS?

    本文发布于:2023-11-01 19:37:21,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1550223.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何将   重定向   www   URL   https

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!