---
title: Handling 404 errors
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This page describes the best practices for creating "404 Not Found" pages when developing Xperience sites using the [MVC development model](https://docs.kentico.com/13/developing-websites/mvc-development-overview.md). We recommend handling 404 errors through IIS, which allows you to return your "Not Found" page in the following scenarios:

- The URL does not match any route in the routing table
- A suitable controller is not found
- A suitable controller action is not found
- The status code of the server's response is 404

To set up a 404 error page, you need to do the following steps:

1. Set up the error handling via IIS in the _Global.asax_ file.
   - The server's IIS must have the **Common HTTP Features -> HTTP Errors** feature installed (in the operating system's features).
2. Create a 404 Not Found page as:
   - an [MVC page](#handling404errors-mvc)\
     – OR –
   - a [static HTML page](#handling404errors-html)
3. Add an [httpErrors](https://docs.microsoft.com/en-us/iis/configuration/system.webserver/httperrors/) element that specifies the path to your 404 page to the project's _Web.config_ file.

## Setting up 404 error handling via IIS

Open the project's **\~/Global.asax** file and add an **Application\_Error()** method to the application class:

```csharp title="Global.asax"

        protected void Application_Error()
        {
            // Sets 404 HTTP exceptions to be handled via IIS (behavior is specified in the "httpErrors" section in the Web.config file)
            var error = Server.GetLastError();
            if ((error as HttpException)?.GetHttpCode() == 404)
            {
                Server.ClearError();
                Response.StatusCode = 404;
            }
        }


```

## &#x20;Creating MVC pages for 404 errors

The first approach is to create a controller with actions handling error states and reference the respective action in the _Web.config_. This approach enables you to use all MVC features when creating the error page.

1. Create a view that presents the content required by your site's 404 page according to the general MVC best practices.
2. Create a controller for the 404 error page.

   - The controller needs to define an action that sets the response status code to 404.
   - The action must be accessible through a route registered in your project's routing table.

     ```csharp title="Controller: NotFound action"

             public ActionResult NotFound()
             {
                 Response.StatusCode = 404;

                 return View();
             }


     ```
3. Modify the project's **Web.config** file and add 404 error handling under the _system.webServer_ node:

   ```xml title="Web.config"

   <system.webServer>
       ...
       <httpErrors errorMode="Custom" existingResponse="Auto">
           <remove statusCode="404" subStatusCode="-1" />
           <error statusCode="404" path="/HttpErrors/NotFound" responseMode="ExecuteURL"/>
       </httpErrors>
       ...
   </system.webServer>

   ```

   > **Note:** **Path attribute value**
   >
   > The **path** attribute of the __ element needs to match the route of the _NotFound_ action in the controller created in the first step, including the application path if the application is running in a virtual directory. For example, if the URL of the 404 error page is _http://www.example.com/MyWebSite/ErrorPages/NotFound_, the path value would be _"/MyWebSite/ErrorPages/NotFound"_, including the starting slash.

Now whenever a live site visitor requests an invalid URL, the content defined in your view is displayed.

## &#x20;Creating static HTML pages for 404 errors

A different approach is to create a static HTML page and reference it in the _Web.config_. Choosing this approach may be easier in some cases, but you may need to manually style the page so that it matches your site's layout.

1. Create a HTML file within your project, with the content required by your site's 404 page.
2. Modify the project's **Web.config** file and add 404 error handling under the _system.webServer_ node:

   ```xml title="Web.config"

   <system.webServer>
       ...
       <httpErrors errorMode="Custom" existingResponse="Auto">
           <remove statusCode="404" subStatusCode="-1" />
           <error statusCode="404" path="HttpErrors\404.html" responseMode="File"/>
       </httpErrors>
       ...
   </system.webServer>

   ```

   > **Note:** **Path attribute value**
   >
   > The **path** attribute of the __ element needs to be a file path relative to the application root. The path must be delimited by backslashes (**\\**) and cannot start with a backslash.

Now whenever a live site visitor requests an invalid URL, the content defined in your HTML file is displayed.
