1

I am trying to redirect the 404 page from web config when users request a non existent page. This is my web config.

When I try it the redirect is not working and I am getting the IIS default 404 page. I am trying it on my local IIS Express.

And this is my page list.The 404 page is locating on the root.

How can I do this?

MFB
  • 19,017
  • 27
  • 72
  • 118
Mesut Başaran
  • 947
  • 6
  • 12
  • 1
    Please copy/paste your web.config here and not show it on image. Is not help to view images, also the image may not exist after some days, and can not even help crawlers to index this page. – Aristos Jan 03 '14 at 21:20
  • To make this work you must try to call asp.net page, so pass from asp.net and follow what you have give him as instruction on web.config. Do you have try that ? or you give pages that not pass from asp.net ? See this one: http://stackoverflow.com/questions/11045733/404-redirecting-for-non-aspx-pages – Aristos Jan 03 '14 at 21:24
  • I have tried the code that you gived but not working. I added this code in web config but now the site is navigating to a completely blank page. 404 page is working if a request a url with extension .aspx .The code i have added in webconfig is – Mesut Başaran Jan 04 '14 at 00:07

4 Answers4

0

IIS is not very good. (Disclaimer statement)

In order for IIS to realize you know what you are doing and actually display your custom 404 page, the page itself must be over a certain size in bytes (512 bytes to be exact), otherwise IIS assumes it knows better and displays it's default 404.

Try adding commented out lines at the bottom of your 404 page to "bloat" it a little. Sounds insane, I know, but it works.

EDIT: 512 bytes I believe is the minimum page size, otherwise IIS skips it. I'm sure someone at M$ thought this was a good idea.

SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
0

On error status code 404 set redirect to "~/404.aspx" this should do the trick.

If it still doesn't work, please try the following:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="~/404.aspx" responseMode="ExecuteURL" />
</httpErrors>

Put the code above inside system.webServer tag in web.config.

IIS Express uses system.webserver, because is was based on IIS7 codebase: http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx.

Please, let me know if it works.

Regards.

João Pinho
  • 3,725
  • 1
  • 19
  • 29
0

Looking at your web.config entry makes me sceptic about whether your error page 404.aspx itself may be the reason you are seen the 404 error. Probably removing / from redirect="/404.aspx" would do the trick. Analyse your request with some dev tool.

Shashank Chaturvedi
  • 2,756
  • 19
  • 29
0
    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <location path="index.aspx">
    <system.webServer>
      <httpRedirect enabled="true" destination="~/Admin/Login.aspx" exactDestination="true" httpResponseStatus="Found" />
      <modules runAllManagedModulesForAllRequests="true"></modules>
    </system.webServer>
  </location>
  <location path="images">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="js">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Resimler">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Script">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Scripts">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <system.web>
    <customErrors mode="On" defaultRedirect="~/Hata.aspx">
      <error statusCode="404" redirect="~/404.aspx"/>
    </customErrors>
    <pages validateRequest="false">
      <controls>
        <add tagPrefix="CKEditor" assembly="CKEditor.NET" namespace="CKEditor.NET"/>
      </controls>
    </pages>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms name=".ASPXAUTH" loginUrl="Admin/Login.aspx" defaultUrl="Default.aspx" protection="All" timeout="30" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" ticketCompatibilityMode="Framework20">
        <credentials passwordFormat="SHA1">
          <user name="admin" password="7dc959a7d6440baa02e0bb84b1dffeb173facf5a" />
        </credentials>
      </forms>
      <passport redirectUrl="internal" />
    </authentication>
    <httpRuntime targetFramework="4.5" requestValidationMode="2.0" executionTimeout="1200" maxRequestLength="51200" />
  </system.web>
  <connectionStrings>
    <add name="InsaatEntities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="ModelInsaatEntitiy" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DB120807190835Entities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="ModelInsaatEntities" connectionString="metadata=res://*/ModelInsaat.csdl|res://*/ModelInsaat.ssdl|res://*/ModelInsaat.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssql03.natro.com;initial catalog=DB120807190835;persist security info=True;user id=USR120807190835;password=PSS!H3Z7F1%;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>

</configuration>

This is my web config .When i request the nonexist.aspx it redirects the page 404.aspx properly but when i request mydomain/nonexists without .aspx extension it redirects the default iis 404 page.My page size is over 512 bytes

Mesut Başaran
  • 947
  • 6
  • 12