首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将浏览器重定向到IIS-8 web.config中URL中的新版本号(目录)?

如何将浏览器重定向到IIS-8 web.config中URL中的新版本号(目录)?
EN

Stack Overflow用户
提问于 2016-12-06 17:03:11
回答 2查看 434关注 0票数 1

我尝试使用这个web.config (IIS-8):

代码语言:javascript
复制
<?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 (连同可能出现在原始请求中的任何查询字符串)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-14 20:16:45

这样做非常简单,如下所示:

代码语言:javascript
复制
<?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)重定向和保留查询字符串。

代码语言:javascript
复制
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&amp;e=f">here</a></body>

使用<match>标记的好处是,输入url将包含应用路径右侧的所有内容,因此即使帮助站点位于虚拟目录(例如,在开发人员的机器上),它也能工作。

票数 1
EN

Stack Overflow用户

发布于 2016-12-09 20:45:32

我完全是从记忆中写的,所以如果它不是100%正确的话,我很抱歉。我非常乐意在评论中讨论。

首先,我们不需要<match/>语句,因为我们使用的是条件。

其次,与HTTP_HOST匹配查找匹配的“1.2URL”是不正确的。HTTP_HOST只包含URI的主机部分,即help.mysite.com。相反,我们需要与REQUEST_URI匹配,并且很可能需要模式^/1\.2\/(.*)

你写的动作似乎是正确的。

试试看会发生什么。

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41000906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档