web.config:redirect 301无法正常工作

时间:2015-12-10 09:01:00

标签: redirect web-config http-status-code-301

我想对一个网站上的所有请求(也包括图片,文档和其他文件)进行301重定向到另一个网站的主页

我尝试将web.config放在网站的根目录上,然后插入此代码:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <location>
    <system.webServer>
        <httpErrors existingResponse="PassThrough" />
      <httpRedirect enabled="true" destination="http://www.newsite.com/" httpResponseStatus="Permanent" />
    </system.webServer>
 </location>
</configuration>

但是在web.config上有这个代码,问题是如果我数字: http://www.oldsite.com/file.html

浏览器会将我重定向到:http://www.newsite.com/file.html

但我需要所有重定向都在主页上。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要设置exactDestination="true"以使目标成为绝对目标:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <location>
    <system.webServer>
        <httpErrors existingResponse="PassThrough" />
      <httpRedirect exactDestination="true" enabled="true" destination="http://www.newsite.com/" httpResponseStatus="Permanent" />
    </system.webServer>
 </location>
</configuration>

请参阅:https://www.iis.net/configreference/system.webserver/httpredirect

相关问题