如何将浏览器重定向到IIS-8 web.config中URL中的新版本号(目录)?
问题描述:
我用这个web.config文件(IIS-8)的尝试:如何将浏览器重定向到IIS-8 web.config中URL中的新版本号(目录)?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
</staticContent>
<rewrite>
<rules>
<rule name="Old version to new" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^help\.mysite\.com\/1\.2\/(.*)" />
</conditions>
<action type="Redirect" url="http://help.mysite.com/1.3/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
...
</system.webServer>
</configuration>
试图加载网站的任何部分时,此配置产生500错误。
我只想更新URL中的版本号,以便每个人都可以继续尝试访问的内容,只需要更新的版本即可。一个完整的链接可能是: http://help.mysite.com/1.2/Content/Widgets/installingWidgets.htm
理想的情况下,当服务器开始担任该页面时,它改为应用重定向规则和客户端结束:
http://help.mysite.com/1.3/Content/Widgets/installingWidgets.htm(与任何查询字符串以及可能有原来的请求中存在)
答
这是非常简单的做如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect 1.2 to 1.3" stopProcessing="true">
<match url="^1\.2/(.*)" />
<action type="Redirect" url="1.3/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
我测试这和它做一个永久(301)重定向,并保留查询字符串。
GET http://localhost/1.2/a/b?c=d&e=f HTTP/1.1
User-Agent: Fiddler
Host: localhost
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: http://localhost/1.3/a/b?c=d&e=f
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Wed, 14 Dec 2016 20:25:11 GMT
Content-Length: 159
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://localhost/1.3/a/b?c=d&e=f">here</a></body>
有关使用<match>
标签的好处是,输入的URL将包括一切的application path的权利,所以这将工作,即使帮助网站位于一个虚拟目录,例如,在开发人员的机器上。
答
我从内存中完全写这个,所以我很抱歉,如果它不是100%正确的。我很乐意在评论中讨论。
首先,我们不需要<match/>
声明,因为我们使用的是条件。
其次,匹配HTTP_HOST以查找匹配的“1.2”URL是不正确的。 HTTP_HOST只包含URI的主机部分,即help.mysite.com。相反,我们需要与REQUEST_URI匹配,并且我们可能需要^/1\.2\/(.*)
的模式。
您所写的操作看起来是正确的。
给这个镜头,看看会发生什么。
<rule name="Old version to new" stopProcessing="true">
<conditions>
<add input="{REQUEST_URI}" pattern="^/1\.2\/(.*)" />
</conditions>
<action type="Redirect" url="http://help.mysite.com/1.3/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>