---
title: Creating custom error handling pages
---

> 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).

You can configure the system to display custom pages instead of standard error messages. Custom pages help reduce the inconvenience caused to visitors if they run into an error while browsing your website, and also improves the security of the site by hiding potentially sensitive internal data (such as code in stack traces). You can create custom pages for this purpose with any kind of content, such as an apology or additional instructions, and then configure the system to display the pages in the appropriate situations.

## Adding custom Page not found error pages

The _Page not found_ error (404 HTTP status code) is one of the most common problems encountered by visitors. Kentico provides several features that allow you to conveniently set up a custom page as a response. For _page not found_ errors, the error page can either be a physical _.aspx_ file placed under the web project or a dedicated page created in a specific website's content tree.

To assign your custom page to a particular website (or globally):

1. Go to **Settings -> Content**.
2. Enter the URL of the given page as the value of the **Page not found URL** setting, for example: _\~/SpecialPages/PageNotFound.aspx_

   > **Info:** Since there are two possible types of error pages, the system interprets the URL in two different ways. The sample URL value above specifies either:
   >
   > - The URL of a physical page named _PageNotFound.aspx_ located in the web project under a folder called _SpecialPages_.
   > - If such a file does not exist, the system attempts to select a Kentico page under the current website, with an alias path equal to _/SpecialPages/PageNotFound_.
3. Click **Save**.

It is recommended to use Kentico pages for page not found error pages. With this approach, you can define the error page's content using the portal engine and leverage all of its features. For instance, you can translate the page not found page on a [multilingual website](https://docs.kentico.com/k11/multilingual-websites.md) and Kentico automatically displays the culture version that matches the language selected by the user.

You do not need to manually handle the HTTP response code of the page specified by the setting. The page automatically returns a 404 status code when accessed (applies to both pages and physical pages). This allows applications, services and web crawlers to find out that a _page not found_ error has occurred.

> **Note:** **Handling 404 errors for general content**
>
> Perform the following steps to ensure that the system returns your custom _Page not found_ error page for invalid requests that target all types of site content, not just the pages processed by the Kentico engine:
>
> 1. Edit your application's **web.config** file.
> 2. Find the **system.webServer** section directly under the root (i.e. not under a specific __ element).
> 3. Set the **runAllManagedModulesForAllRequests** attribute to _true_ for the opening tag of the **** element:
>
> ```html
>
> <system.webServer>
>   <modules runAllManagedModulesForAllRequests="true">
>     ...
>   </modules>
>
> ```

## Handling general errors

Kentico is a standard ASP.NET application, so you can configure the handling of all types of errors and exceptions via the **** element under the __ section of your web.config file. See the [customErrors](https://msdn.microsoft.com/en-us/library/h0hfz6fc%28v=vs.85%29.aspx) MSDN article for more information.

By default, the Kentico web.config sets a general error page for errors with the 500 HTTP status code. The error page is displayed only for remote clients, requests from local development machines return errors with full details.

You can add any number of child **** elements representing individual types of HTTP errors that you wish to handle.

- You need to enter the HTTP response code of the given error into the **statusCode** attribute and the URL of the appropriate error page as the **redirect** value.
- It is recommended to add general error pages directly into your web project as _.aspx_ files, for example under the **CMSMessages** folder.
- You can define the content of the error page to match your specific requirements.
- Keep in mind that your custom error pages should always return the appropriate _HTTPResponse_ status code.

```xml title="Example"

<system.web>
...
    <customErrors mode="RemoteOnly">
        <error statusCode="500" redirect="~/CMSMessages/CustomError.aspx" />
        <error statusCode="503" redirect="~/CMSMessages/CustomError.aspx" />
    </customErrors>
...
</system.web>

```

When using _,_ it is also recommended to disable handling of errors on the level of IIS by adding a **httpErrors** element under the application's main __ element, with the **existingResponse** attribute set to _PassThrough_:

```html

<system.webServer>
...
    <httpErrors existingResponse="PassThrough" />
...
</system.webServer>

```

> **Info:** **Using  for error handling**
>
> Alternatively, handling of errors can also be configured on the IIS level via the **** element under your web.config's main **** element. See the [HTTP Errors](https://www.iis.net/configreference/system.webserver/httperrors) article for more information.
>
> Choose and test which approach works best, depending on the environment in which your application is running.

> **Note:** **Before deployment**
>
> Set custom error messages as described in this procedure _before_ deploying your website and going live. Also, remember to [disable debugging and tracing for your ASP.NET application](https://docs.kentico.com/k11/securing-websites/deploying-websites-to-a-secure-environment/web-config-file-settings.md#error-messages-and-disabling-the-debug-and-trace).

### Page not found error conflicts

If you set the **defaultRedirect** attribute of the **customErrors** element in your web.config file, the default redirect may override the page assigned for 404 page not found errors in the [Kentico settings](#adding-custom-page-not-found-error-pages). This applies to URLs ending with the _.ashx, .asmx_ or _.svc_ extensions.

To work around this behavior, you can set the **redirectMode** attribute to _ResponseRewrite:_

```html

<customErrors defaultRedirect="~/CMSMessages/CustomError.aspx" mode="RemoteOnly" redirectMode="ResponseRewrite">

```
