Copied to clipboard

Flag this post as spam?

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


  • Jordi Bartolomé 21 posts 41 karma points
    Aug 13, 2013 @ 12:26
    Jordi Bartolomé
    0

    Force 404 redirection from another node

    Hi,

    I'm using Umbraco 4.9.0 and I want to redirect some nodes without template to the 404 error page. To do that, I've assigned a template that makes a redirection to the 404 node.

    To make the redirection I use HttpContext.Current.Response.StatusCode = 404; HttpContext.Current.Response.Redirect(redirectPath, false);

    What it actually does is a first 302 redirection and then a 404, which is not what I'm actually looking for.

    I'd like to know if there is a way to call the default 404 umbraco redirection, the one which is automatically executed when someone tries to access to a non-existing node.

    Thanks in advance,

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 10:14
  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 10:19
    Jordi Bartolomé
    0

    Hi Dave, thanks for replying.  

    These handlers fire automatically when a page is not found. I want to simulate that a page is not found even it exists, so I want to fire it manually.  

    Do you hve any idea on how?

     

    Thanks!

     

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 10:27
    Dave Woestenborghs
    0

    If you configure these. Also pages without a template assigned should trigger a 404

    Dave

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 10:34
    Jordi Bartolomé
    0

    I'm using Umbraco 4.9.0. In this version, pages without template trigger a very ugly error, not a 404.

    I know that in later versions it's fixed triggering a 404, but not in 4.9.0. In most forums recommend to create an "Error template" to assign to nodes without template.

     

    In my code I have a custom 404 handler as the links say, and it works perfectly when a 404 is triggered. The problem is that I need to fire a 404 event manually, and I don't have any idea on how :(

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 10:43
    Dave Woestenborghs
    0

    You can set a the id of a page in the umbracosettings.config that will show when a page without a template is called. 

     

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 10:48
    Jordi Bartolomé
    0

    Yo know where?

    I have:

    <error404>
     <errorPage culture="default">1234</errorPage>
     <errorPage culture="en-GB">2345</errorPage>
     ...
    </error404>
    

    Where can I set the id you said?

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 10:57
    Dave Woestenborghs
    0

    I don't have a specific culture setup

    I have it like this : 

    <errors>

    <error404>myid</error404>

    </errors>

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 11:02
    Jordi Bartolomé
    0

    You only have a "default" id page for all errors, but it's similar as what I have.

    What version of Umbraco are you using? If you use 4.9 or below, I don't believe pages without template trigger 404 in your computer. It was a "bug" of Umbraco and, as far as I know, it was solved in 4.10.

    If it works on your computer, please let me know how... :)

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 11:04
    Dave Woestenborghs
    0

    I have this setup on 4.7.2 and it works there

    Can't you do a upgrade instead of find a workaround ?

    Dave

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 11:12
    Jordi Bartolomé
    0

    I can't upgrade it. I need this version for the moment :( I'll try it again, but I don't understand how it works in 4.7.2!

    Thanks anyway! Jordi

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 11:15
    Dave Woestenborghs
    0

    Maybe the error is related to setting error pages per culture ? Because I don't have a culture set and it works.

    Dave

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 11:18
    Dave Woestenborghs
    0

    Another option ofcourse is create your own 404 handler with the bug fixed. You can get the source of the lastest version here.

    https://github.com/umbraco/Umbraco-CMS/blob/e41d3c7e844b63780a4842ccd105e8bb593e4d54/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs

     

    public class handle404 : INotFoundHandler {
            #region INotFoundHandler Members
    
            private int _redirectID = 0;
    
            public bool CacheUrl {
                get {
                    return false;
                }
            }
    
            public bool Execute(string url) {
                try
                {
                    LogHelper.Info(string.Format("NotFound url {0} (from '{1}')", url, HttpContext.Current.Request.UrlReferrer));
    
                    // Test if the error404 not child elements
                    string error404 = umbraco.library.GetCurrentNotFoundPageId();
    
    
                    _redirectID = int.Parse(error404);
                    HttpContext.Current.Response.StatusCode = 404;
                    return true;
                }
                catch (Exception err)
                {
                    LogHelper.Error("An error occurred", err);
                    return false;
                }
            }
    
            public int redirectID {
                get {
                    return _redirectID;
                }
            }
    

     

    Dave

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 12:11
    Jordi Bartolomé
    0

    That's actually what I have in my code, and it's used to decide which error page must be shown depending on the culture. But this doesn't trigger any 404 error page, this is executed when the "404 error page event" is fired.

    Could you tell me what you have in your /config/404handlers.config file, please?

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 13:25
    Dave Woestenborghs
    0

    My 404config is the default one? 

     

    Maybe you can post your config and code ?

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 16:25
    Jordi Bartolomé
    0

    This is my file:

     

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

    <NotFoundHandlers>

      <notFound assembly="InfoCaster.Umbraco._301UrlTracker" type="Handler301URLTracker" />

      <notFound assembly="umbraco" type="SearchForAlias" />  

      <notFound assembly="umbraco" type="SearchForProfile" />

      <notFound assembly="UmbracoExtensions" type="Custom404" />

    </NotFoundHandlers>

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 14, 2013 @ 16:36
    Dave Woestenborghs
    0

    I don't see your file content

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 14, 2013 @ 16:42
    Jordi Bartolomé
    0

    sorry! 

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Aug 15, 2013 @ 08:18
    Dave Woestenborghs
    0

    Can you post the code of your file handler ?

  • Jordi Bartolomé 21 posts 41 karma points
    Aug 19, 2013 @ 11:14
    Jordi Bartolomé
    0

    I finally solved my problem. I created a function that simulates the 404 error. I did it by doing the following:

        public static void RedirectToErrorPage(string culture)
        {
            XmlNode error404Node = UmbracoSettings.GetKeyAsNode("/settings/content/errors/error404");
            XmlNode cultureErrorNode = error404Node.SelectSingleNode("errorPage [contains(@culture, '" + culture + "')]");
            if (cultureErrorNode == null)
            {
                cultureErrorNode = error404Node.SelectSingleNode("errorPage [contains(@culture, 'default')]");
            }
    
            int nodeId;
            bool success = int.TryParse(cultureErrorNode.FirstChild.Value, out nodeId);
            string errorTemplate = library.RenderTemplate(nodeId);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("Content-Length", errorTemplate.Length.ToString());
            HttpContext.Current.Response.Write(errorTemplate);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
        }
    

    That was the only way I found to render another page without using "Server.Transfer", which doesn't work in Umbraco, and without changing the URL using Response.Redirect.

    I hope this helps!

Please Sign in or register to post replies

Write your reply to:

Draft