Copied to clipboard

Flag this post as spam?

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


  • Camron Tucker 11 posts 31 karma points
    Oct 28, 2011 @ 18:07
    Camron Tucker
    0

    404 redirect to external site

    We have a website that on a 404 error we need to redirect to a slightly different url, i.e. www.domain1.com/test on a 404 needs to redirect to www.domain2.com/test. It needs to preserve everything after the .com. What would be the best way to handle this in Umbraco?

  • Rich Green 2246 posts 4006 karma points
    Oct 28, 2011 @ 18:49
    Rich Green
    0

    Hey Camron,

    I'm pretty sure you would have to add your own HTTPhandler to accomplish this, shouldn't be too tricky.

    Maybe download the uComponents source and have a look at the source for http://ucomponents.codeplex.com/wikipage?title=SearchForPageNotFound

    Rich

  • Mike Chambers 621 posts 1203 karma points c-trib
    Oct 28, 2011 @ 23:00
    Mike Chambers
    0

    or write your own custom 404 handler something like this... (http://our.umbraco.org/wiki/how-tos/how-to-implement-your-own-404-handler was were I got the inspiration)

    NB untested code below...

    public class Custom404Redirect : INotFoundHandler
        {
            #region INotFoundHandler Members

            public bool CacheUrl
            {
                get
                {
                    return false;
                }
            }

            public bool Execute(string url)
            {
                // add entry to the log for debug purposes.
                umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.NotFound, _redirectID, "404 Redirect - " + HttpContext.Current.Request.Url);

                // can set as a 301 or anything else depending on what you want

                //HttpContext.Current.Response.Status = "301 Moved Permanently";

    // might need to revisit this to your own requirements.
                HttpContext.Current.Response.AddHeader("Location", "WWW.DOMAIN2.COM/"+HttpContext.Current.Request.Url);

                return true;
            }

            #endregion
        }

     

    and add to the 404handlers.config

    <?xml version="1.0" encoding="utf-8" ?>
    <NotFoundHandlers>

    ...other handlers...
      <notFound assembly="XXX" type="Custom404" />

    .. leave the umbraco custom handler to catch all
        <notFound assembly="umbraco" type="handle404"/>
    </NotFoundHandlers>

  • Jan Skovgaard 11258 posts 23500 karma points MVP 7x admin c-trib
    Oct 28, 2011 @ 23:03
    Jan Skovgaard
    0

    Hi Camron

    I'm not sure but maybe you could add a rewriting rule in the UrlRewriting.config file, which is found in the "config" folder? I'm not expert with url rewriting at all just thought that I would mention that it might be possible?

    /Jan

  • Camron Tucker 11 posts 31 karma points
    Oct 31, 2011 @ 18:04
    Camron Tucker
    0

    Thanks everyone for your replies. I ended up writing my own 404 handler implementing the INotFoundHandler like Mike and Rich suggested, which was pretty slick and easy.

  • Atoosa Khoda 86 posts 138 karma points
    Feb 04, 2013 @ 05:32
    Atoosa Khoda
    0

    Hi There Camron. I'm having a similar senario, would like to redirect to an external domain via 404 handler. What did you set the _redirectID? Generally my handler is working if i redirect to an node within the umbraco, but can't get it working for an external 301 with no redirect id.

    This is what i've used:

    HttpContext.Current.Response.StatusCode = 301;

      HttpContext.Current.Response.Status = "301 Moved Permanently";

    // might need to revisit this to your own requirements.
            HttpContext.Current.Response.AddHeader("Location", "WWW.DOMAIN2.COM/somepage");

                return true;

    Any luck?

    Thanks.

  • SinkyPars 132 posts 175 karma points
    Jun 09, 2013 @ 20:21
    SinkyPars
    0

    Bit of a bump here, I too have a similar issue. If the domain is not in my host names I would like to show a 404, can anyone please advise on how to do this?

    Thanks

    Scott

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies