0

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>
S.C.
  • 900
  • 1
  • 13
  • 39
  • have a look here, maybe this can help you further [Convert isapi rewrite rules to IIS 7 rewrite](http://stackoverflow.com/questions/20038145/convert-isapi-rewrite-rules-to-iis-7-rewrite) – Mivaweb Nov 27 '14 at 08:07
  • Sorry, after reading the question, I am still unable to set up a correct configuration for my case. – S.C. Nov 27 '14 at 08:25
  • does your provider support ISAPI? Maybe you could try using ISAPI rules in order to redirect unexisting urls to your homepage: [ISAPI DOCS](http://www.isapirewrite.com/docs/) – Mivaweb Nov 27 '14 at 08:27
  • https://www.youtube.com/watch?v=6wObqULbm5g – AnotherGeek Nov 27 '14 at 08:39

1 Answers1

0

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

AnotherGeek
  • 874
  • 1
  • 6
  • 24
  • I have just tried `` without success. `403` is still returned when I access an invalid URL. Any clue? – S.C. Nov 27 '14 at 09:09
  • This solution works if the invalid url begin with http://www.example.com because it does the redirection in the same application which is hosted in an IIS site so in the defaultredirection you must sfecify the local path – AnotherGeek Nov 27 '14 at 09:43