来自内容树的sitecore中的自定义404页面

Sam*_*Sam 4 multilingual sitecore

我有多语种网站,我想要一个所有语言的自定义404页面,这取决于网站的上下文.获取内容树中定义的404页面或从内容树访问404页面的正确方法是什么?如果我将在我的站点根目录中定义,而不是从内容树中定义,我可以获得404页面.

在此输入图像描述

我想从内容树中获取404页作为我的404自定义重定向.

我的Webconfig设置:

<setting name="ItemNotFoundUrl" value="/404Page.aspx" />
Run Code Online (Sandbox Code Playgroud)

IIS 404条目. 在此输入图像描述

当sitecore中没有页面时我得到的错误:

将IIS更改为默认值并设置httpErrors之后

  <error statusCode="404" path="/404PAGE.aspx" responseMode="ExecuteURL" />

</httpErrors>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

tec*_*414 6

Sitecore做了一些自己的"未找到项目"处理,也没有很好地以SEO友好的方式处理404s.

我发现在Sitecore中处理404s和其他错误的最佳解决方案是Sitecore错误管理器.

http://marketplace.sitecore.net/en/Modules/Sitecore_Error_Manager.aspx

https://github.com/unic/SitecoreErrorManager/wiki


jam*_*kam 6

您可以将ItemNotFoundUrlSitecore配置设置为内容树中的任何项目,它不需要位于站点根目录中,只要您不指定语言,它将使用用户浏览该站点的任何内容(或第一次访问的默认值).该项目必须与所有站点在同一树结构中:

<setting name="ItemNotFoundUrl" value="/errors/404.aspx" />
<setting name="LayoutNotFoundUrl" value="/errors/500.aspx"/>
<setting name="NoAccessUrl" value="/errors/403.aspx"/>
Run Code Online (Sandbox Code Playgroud)

Techphoria414是正确的,Sitecore开箱即用并不擅长以SEO友好的方式处理404s,它将执行302重定向到错误页面.但是,如果您将以下内容设置为,true则将使用Server.Transfer:

<!--  USE SERVER-SIDE REDIRECT FOR REQUEST ERRORS
    If true, Sitecore will use Server.Transfer instead of Response.Redirect to redirect request to service pages when an error occurs (item not , access denied etc).
    Default value: false
-->
<setting name="RequestErrors.UseServerSideRedirect" value="true"/>
Run Code Online (Sandbox Code Playgroud)

这会触发ExecuteRequest管道中的以下代码.

protected virtual void PerformRedirect(string url)
{
  if (Settings.RequestErrors.UseServerSideRedirect)
    HttpContext.Current.Server.Transfer(url);
  else
    WebUtil.Redirect(url, false);
}
Run Code Online (Sandbox Code Playgroud)

您可以在此博客文章中阅读更多相关信息.确保在处理它的代码中将当前System.Web.HttpResponse的Status属性设置为404.处理HTTP 404文档中有更多信息.

错误管理器是一个很棒的模块,如果您需要为不同的站点设置不同的URL位置,它会更加灵活.


Ruu*_*ier 6

ExecuteRequesthttpBeginRequest管道中使用处理器的自定义实现.
这就是最终处理404请求的地方.

您可以RedirectOnItemNotFound在其中覆盖该方法并添加一些逻辑以在每个站点加载不同的404页面.

看一下这篇博文,解释如何实现它.

编辑:我添加了一个如何实现它的示例,以便您可以返回特定于站点的404页面.

如果进行此修改,则可以为每个站点返回不同的404页面:

将其添加到配置中:

<setting name="NotFoundPage.SiteName1" value="/not-found.aspx" />
<setting name="NotFoundPage.SiteName2" value="/not-found.aspx" />
Run Code Online (Sandbox Code Playgroud)

然后在自定义RedirectOnItemNotFound代码中,执行此操作以返回特定于站点的404内容:

public class ExecuteRequest : Sitecore.Pipelines.HttpRequest.ExecuteRequest
{
    protected override void RedirectOnItemNotFound(string url)
    {
        var context = System.Web.HttpContext.Current;

        try
        {
            // Get the domain of the current request.
            string domain = context.Request.Url.GetComponents(UriComponents.Scheme | UriComponents.Host, UriFormat.Unescaped);

            // Get 'not found page' setting for current site.
            string notFoundUrl = Sitecore.Configuration.Settings.GetSetting(string.Conact("NotFoundPage.", Sitecore.Context.Site.Name));

            // Request the contents of the 'not found' page using a web request.
            string content = Sitecore.Web.WebUtil.ExecuteWebPage(string.Concat(domain, notFoundUrl));

            // Send the content to the client with a 404 status code
            context.Response.TrySkipIisCustomErrors = true;
            context.Response.StatusCode = 404;
            context.Response.Write(content);
        }
        catch (Exception)
        {
            // If our plan fails for any reason, fall back to the base method
            base.RedirectOnItemNotFound(url);
        }

        // Must be outside the try/catch, cause Response.End() throws an exception
        context.Response.End();
    }
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是您使用站点名称作为设置键,以便您可以解析每个站点的配置值.
代码当然需要一些工作,但你明白了......

编辑2:添加了用新的管道处理器替换原始管道处理器的示例配置:

<pipelines>
  <httpRequestBegin>
    <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel">
      <patch:attribute name="type">ParTech.Pipelines.ExecuteRequest, ParTech</patch:attribute>
    </processor>
  </httpRequestBegin>
</pipelines>
Run Code Online (Sandbox Code Playgroud)