Copied to clipboard

Flag this post as spam?

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


  • Michael 6 posts 28 karma points
    May 13, 2013 @ 17:49
    Michael
    0

    301 redirect using a custom 404 handler - executes but has no effect

    Hi there,

    I'd like to redirect some of the paths on our domain that used to point to pages we had before using Umbraco, to the location of the new pages. We want to use redirection rather than URL rewriting.

    For example, http://mysite.com/blog-old (which currently returns a 404, handled by Umbraco), should redirect to http://mysite.com/blog-new. The new link may not necessarily point to a node in Umbraco (i.e., it may be an external link).

    To get a very general case working, I've written a custom 404 handler based on Morten's suggestion in this thread, which looks like this:

    using System.Web;
    using System.Xml;
    using umbraco;
    namespace MyWebsite.NotFoundHandlers
    {
        public class PermanentRedirectHandler : umbraco.interfaces.INotFoundHandler
        {
            private int _redirectID = -1;
            public bool CacheUrl
            {
                get { return false; }
            }
            public int redirectID
            {
                get { return _redirectID; }
            }
            public bool Execute(string url)
            {
                HttpContext.Current.Response.Status = "301 Moved Permenantly";
                HttpContext.Current.Response.AddHeader("Location", "http://www.google.co.uk/");
                return true;
            }
        }
    }

    And added it into the 404handlers.config here:

    <?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="MyWebsite" namespace="MyWebsite.NotFoundHandlers" type="PermanentRedirectHandler" />
     
    <notFound assembly="umbraco" type="handle404"/>
    </NotFoundHandlers>

    To my understanding, this should just redirect every unmapped URL to Google, and I'm comfortable that I can work in specific pages later.

    However, when I visit an unmapped URL, I always get the Umbraco 404 page. I've confirmed that the handler is being called with some logging, so I know the logic is being executed, but it's not having any effect on the resolution.

    We're using 4.11.1, and I have seen mention of a related bug, which I assume affects us (it doesn't seem to be fixed for our version). Can anyone confirm this is the cause? And if I'm (hopefully) wrong about that, can anyone suggest what I might be doing wrong to prevent the handler from working?

  • Michael 6 posts 28 karma points
    May 13, 2013 @ 18:11
    Michael
    0

    I thought I'd also mention, I've tried adding the following lines to web.config (in place of the approach above):

    <configuration>
      ...
     
    <locationpath="~/blog-old">
     
    <system.webServer>
     
    <httpRedirectenabled="true"destination="http://www.google.co.uk"httpResponseStatus="Permanent"/>
     
    </system.webServer>
     
    </location>
    </configuration>

    However, this just gave me the regular 404 pages too. I'd be just as happy to use this approach if there's a way to make it work (or any other approach that works, of course :p).

     

Please Sign in or register to post replies

Write your reply to:

Draft