This article describes how to create a custom 404 error page in Ektron CMS that will return a HTTP status code 404, use a friendly aliased URL and give content authors the ability to change the content within the page.
Create the template
Create a new ASPX Web Form template called Error404.aspx (or something similar). Add a ContentBlock to the HTML source view and set the DynamicParameter property to “id”.
Add the template to the CMS in the Settings > Configuration > Template Configuration section in the Ektron workarea.
Now create a new content item in the CMS and assign it to the Error404.aspx template that you created earlier. Assign an alias to the content item, something like “/page-not-found/”.
Test the page loads of by browsing directly to /page-not-found/.
HTTP status code 404
Now you need to tell the template to return the correct HTTP status code. At present it is returning a status code of 200 which means “success”. This will cause search engines to incorrectly believe that your 404 page is actually valid content – in other words, it will be indexed and returned in Google’s results!
To prevent this, go to the code-behind file (this is in C# of course):
protected void Page_PreRender(object sender, EventArgs e) { Response.TrySkipIisCustomErrors = true; Response.Status = "404 Not Found"; Response.StatusCode = 404; }
What this does is set the HTTP status code to be 404. This will indicate to search engines that they have found a missing page so they will not index it.
You can test your page by browsing to an invalid page and seeing if the custom error page is returned.
Enable custom error handling
The next step is to tell .Net that we want to use a custom error page. This is done within the web.config file like so :
<system.web> <customErrors mode="On"> <error statusCode="404" redirect="/page-not-found/" /> </customErrors> <system.web>
This tells .Net to send any “404 page not found” errors to an alias called “/page-not-found/”.
Problems with invalid aliases
You may find that your custom 404 error page isn’t being activated if you try to browse to an invalid alias. What I have found is that you instead get a nasty server 500 error instead:
Server Error in Application “###”
Internet Information Services 7.5
HTTP Error 500.0 – Internal Server Error
Internal Server Error
Module | ManagedPipelineHandler |
---|---|
Notification | ExecuteRequestHandler |
Handler | ExtensionlessUrlHandler-Integrated-4.0 |
Error Code | 0x800703e9 |
Requested URL | http://###:80/wibble/ |
---|---|
Physical Path | C:\inetpub\wwwroot\###\wibble\ |
Logon Method | Forms |
Logon User | ### |
Eeek! Where is my custom error page?!?!
I discovered that the problem was due to an incorrect setting in my web.config file. I needed to remove a handler called “ExtensionlessUrlHandler-Integrated-4.0”, like so :
<system.webServer> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> </handers> </system.webServer>
(Please note I am using IIS 7)
By removing this HTTP handler it allows the request to be passed through to your custom 404 error page instead.