Wildcards in Sitecore are an interesting module, because they provide the possibility to construct an extensive and complex data model for your website. But there are also many downsides to working with the Wildcard module, and one of them is handling 404 pages, or cases when the wildcard item is not found. By default, if the wildcard item is not found, the page will just not display the data that is encapsulated in the item. Or, throw an exception, if you didn’t anticipate this scenario.
In a website with a complex data model behind it, you have to do 4 things in order to enable 404 pages for invalid data input.
1. Make the Wildcard token aware of the datasource that will replace it in the URL.
2. Hook into the Sitecore pipeline before the context item is resolved
3. Look into the Token’s datasource and determine if an item with the requested name exists.
4. If not, set the context item to your 404 page item.