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?
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);
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?
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.
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");
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?
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?
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
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>
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
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.
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.
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
is working on a reply...