使用 IIS URL 重写从地址中删除 www 的正确方法 [英] Proper method to remove www from address using IIS URL Rewrite

查看:21
本文介绍了使用 IIS URL 重写从地址中删除 www 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 IIS URL 重写从 url 中删除 www 子域的最佳方法是什么?

What is the optimal way to remove the www subdomain from a url using IIS URL Rewrite?

推荐答案

如果您希望它与任何主机名一起使用(而不是将其硬编码到规则中),您需要执行以下操作:

If you want it to work with any hostname (not hardcoding it into the rule), you'd want to do something like this:

<rule name="Remove www" stopProcessing="true">
  <match url="(.*)" ignoreCase="true" />
  <conditions logicalGrouping="MatchAll">
    <add input="{HTTP_HOST}" pattern="^www.(.+)$" />
  </conditions>
  <action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

在重定向操作中,{C:1} 包含条件中的第二个捕获组,而 {R:0} 包含规则中的任何内容(路径).appendQueryString="true" 还将任何查询字符串附加到重定向(如果存在).但请记住,任何 url 哈希(如果存在)都将在此过程中丢失,因为它们不会传递到服务器.

in the redirect action, the {C:1} contains the second capturing group in the condition, whereas the {R:0} contains whatever was in the rule (the path). appendQueryString="true" will also append any querystring to the redirect (if present). Keep in mind though, that any url hashes, if present, will be lost in the process since those don't get passed to the server.

这篇关于使用 IIS URL 重写从地址中删除 www 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆