简体   繁体   中英

Blocking invalid URL and redirect to homepage in IIS

Currently I am using the web hosting service provided by another company. It uses IIS 7/IIS 8 and allows customers to modify web.config according to their needs.

Suppose I have set up two valid URL:

  1. http://www.example.com
  2. http://dev.example.com

Now, when I try to access http://abc.example.com , HTTP Error 403.14 - Forbidden is returned; and if I then try to access http://www.example.com/abc , HTTP Error 404.0 - Not Found is returned.

How can I configure web.config such that users will be redirected to http://www.example.com when they are trying to access an invalid URL?

I have tried to use the snippet provided by the company but without success:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Canonical Host Name" stopProcessing="true">
                  <match url="(.*)" />
                  <conditions>
                    <add input="{HTTP_HOST}" pattern="^example\.com$" />
                  </conditions>
                  <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
                </rule>
             </rules>
        </rewrite>
    </system.webServer>
</configuration>

In system.web in webconfig tag add this tag

<customErrors mode="On" defaultRedirect="~/Home/Index"> 
 </customErrors>

This will redirect you to the home page whenever an error occured You can be more specific with redirection by adding in customErrors tag

<error statusCode="401" redirect="/error/otherpage"/>

This will indicate the page that you will be redirected to when 401 error occurs

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