REDIRECT TO PARENT TEMPLATE

It's quite common in a data centric CMS like Umbraco to have various organised content that is combined in some way to build a single page that is displayed to the user. It's this architecture that provides the power and flexibility over a page based CMS.

This kind of content may not mean much by itself or is simply too minor to warrant showing it on a page of its own so it will quite often have no template assigned to it.

So what happens if a user manages to navigate to one of these items? Well they are going to see a nice 404 page if you've configured one or a rather ugly one generated by Umbraco if you've not.

Depending on what you're trying to do and how your content is organised it may be much nicer for the end user if they were redirected automatically to the parent page of the item they had requested.

Using this website as an example you can see that portfolio testimonial content is stored under the portfolio item itself.

When a portfolio page is requested a macro on the page runs to get and display its testimonials. It's simple and very standard.The UI design of the site is that we display the testimonials along with the rest of the portfolio content on the portfolio page itself. We are not interested in displaying a few lines of testimonial text on a page by itself, that would be boring so we've not created a template for it.

So we need a way to redirect the user to the portfolio page should they ever make a direct request to see the testimonial item itself.

One solution could be to make use of the 'umbracoRedirect' property as used in this post to make Umbraco perform the redirect, however the onus will be on the content editor to remember to set up the redirect link manually every time. It's another job for them to do and people sometimes forget.

A better solution is to create a 'Redirect to parent' template and then assign this template as the default template for any document types where it makes sense.

Below is an example of a template that uses a simple Razor macro to redirect to the item's parent.

Now, whenever an item is requested for a document type that uses the 'Redirect to parent' template, the user will automatically be redirected to the parent page. This will work for nested items too as the redirects will just bubble on up until we get to a page we want to display.

The best thing is that it's easy for the content editor as they don't even have to think about it.