IIS 7规范URL重定向 [英] IIS 7 Canonical URL redirect

查看:105
本文介绍了IIS 7规范URL重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使网站的地址始终带有www,并通过IIS重写强制实施.

I would like to make a website always have www in the address, and enforce it via IIS rewrite.

例如,test.com将变成www.test.com.

For example, test.com would become www.test.com.

典型的重​​写规则示例是:

The typical example rewrite rule is:

<rule name="Canonical Host Name" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTP_HOST}" negate="true" pattern="^www\.test\.com$" />
  </conditions>
  <action type="Redirect" url="http://www.test.com/{R:1}" redirectType="Permanent" />
</rule>

但是,这需要我输入网站的完整网址.它不适用于具有www.test.dev和www.test.stage之类的URL的开发和暂存环境.

However this requires me to enter the full url of my website. It will not work for development and staging environments that have URLs like www.test.dev and www.test.stage.

是否可以创建一个处理所有这些情况的IIS重写规则?

Is it possible to create an IIS Rewrite rule that will handle all of those cases?

推荐答案

您很正确,完整的URL必须位于web.config中.不过您有选择.

You're right that the full URL needs to be in the web.config. You have options though.

  1. 您可以使用config转换使正则表达式匹配正确的环境.

  1. You can use a config transform to make the regular expression match the correct environment.

如果在web.config中包含所有三个URL重写规则,似乎没有任何危害.听起来您的环境是孤立的,所以每个环境只能匹配其中一个规则.那样会使您的web.config混乱,但不会令人发指.

There doesn't seem to be any harm if you include all three URL rewrite rules in your web.config. It sounds like your environments are isolated so each environment would only ever match one of the rules. That can clutter your web.config, but not horribly.

我会选择选项1.您可以在此处找到有关配置转换的信息: http://msdn.microsoft.com/zh-CN/library/dd465326.aspx

I'd go with option 1. You can find information on config transforms here: http://msdn.microsoft.com/en-us/library/dd465326.aspx

这篇关于IIS 7规范URL重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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