Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Phillip Turner 98 posts 412 karma points
    May 31, 2014 @ 06:48
    Phillip Turner
    0

    Custom 404 handler working locally but not in production

    So i followed the Umbraco guide to making a custom 404 handler as I run multiple domains on my Umbraco instance.

    Web.Config Entries (Dev and Production):

    <!-- <add key="404Domains" value="xyz.umbraco.local,3315;zyx.umbraco.com,3316" /> --> 
    <add key="404Domains" value="www.xyz.com,3315;www.zyx.com,3316" />
    

    404handlers.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <NotFoundHandlers>
    <notFound assembly="umbraco" type="SearchForAlias" />
    <notFound assembly="umbraco" type="SearchForTemplate"/>
    <notFound assembly="umbraco" type="SearchForProfile"/>
    <notFound assembly="umbraco" type="handle404"/>
    <notFound assembly="UmbracoUS1" namespace="UmbracoUS1.Handler" type="URLNotFound" />
    </NotFoundHandlers>
    

    404handler.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.interfaces;
    using System.Configuration;
    
    namespace UmbracoUS1.Handler
    {
        public class URLNotFound: INotFoundHandler
        {
            private int redirectId;
            private string[] domains
            {
                get
                {
                    try
                    {
                        return ConfigurationManager.AppSettings["404Domains"].Split(';');
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
            bool doReturn = false;
    
            public bool Execute(string url)
            {
                var server = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToLower();
    
                foreach (string domain in domains)
                {
                    if (domain.Contains(server))
                    {
                        redirectId = Convert.ToInt32(domain.Split(',')[1]);
                        doReturn = true;
                        break;
                    }
                }
    
                return doReturn;
            }
    
            public bool CacheUrl { get { return false; } }
            public int redirectID { get { return redirectId; } }
        }
    }
    

    The code works fine in development (VS) and my local deployment, but when I put this code into production the 404 is not caught by the handler.

    My local deployment that is working is using the full version of IIS 7 and not express. I do not see anything in the trace logs.

    Has anyone experience the same issue? The only thing that is different from dev to production is the 404Domains key in the web config.

    I have tested the foreach logic in my master templates and the server value is being matched with the domain value coming from the Web.Config appSetting.

    Is there an IIS setting I am missing?

    Slowly pulling my hair out!

    Many thanks

    Phillip

  • Phillip Turner 98 posts 412 karma points
    May 31, 2014 @ 06:59
    Phillip Turner
    100

    So the following added to Web.Config will handle the .aspx pages but not static pages

    <httpErrors existingResponse="PassThrough" />
    

    Any idea how to handle static files?

  • Phillip Turner 98 posts 412 karma points
    Jun 02, 2014 @ 13:33
    Phillip Turner
    0

    Answering my own questions again!

    I did a little more research and added the following rules to the UrlRewriting config

    <add name="catchHtml" 
         virtualUrl="^~(.*).html" 
         rewriterUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="/$1.aspx"
         ignoreCase="true" />
    
    <add name="catchHtm" 
         virtualUrl="^~(.*).htm" 
         rewriterUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="/$1.aspx"
         ignoreCase="true" />
    

    This allowed me to just redirect any html/htm page to a page with the same .aspx file name. Custom 404 then picks it update if its not found. Great side effect is that some pages on the new site have the same file name but with .aspx, so the get served.

Please Sign in or register to post replies

Write your reply to:

Draft