简体   繁体   中英

web.config: redirect 301 doesn't work properly

I want to do a 301 redirect for all the requests on one site (also images, documents, and other files) to the homepage of another site .

I tried putting the web.config on the root of the site, and I'd insert this code:

<?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>

But with this code on the web.config the problem is that if I digit: http://www.oldsite.com/file.html

The browser redirect me to: http://www.newsite.com/file.html

But I need that all the redirects are on the homepage.

How can I do it?

You need to set exactDestination="true" to make the destination absolute:

<?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>

See: https://www.iis.net/configreference/system.webserver/httpredirect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM