【问题标题】:Redirect in a Sitecore solution Noob在 Sitecore 解决方案中重定向 Noob
【发布时间】:2014-10-11 01:57:56
【问题描述】:

在这里我想做的是,我的雇主希望能够使用正则表达式和 Sitecore 中的别名进行 301 重定向,所以我尝试实现它的方式是这样的!

单行文本字段 带有一个复选框来告诉 sitecore 这将是一个正则表达式我是 .NET 和 Sitecore 中的菜鸟,我该如何实现呢?这里是一个例子http://postimg.org/image/lwr524hkn/

我需要帮助我想要处理的重定向示例是这样的,这是我想要做的重定向示例,它可能是解决方案的产品。

example.com/en/solution/platform-features 到

example.com/en/platform-features

我的代码来自http://www.cmssource.co.uk/blog/2011/December/modifying-sitecore-alias-to-append-custom-query-strings-via-301-redirect 这是我想使用正则表达式的查询字符串。

    namespace helloworld.Website.SC.Common
{
    public class AliasResolver : Sitecore.Pipelines.HttpRequest.AliasResolver
    {
        // Beginning of the Methods
        public new void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (!Settings.AliasesActive)
            {
                Tracer.Warning("Aliases in AliasResolver are not active.");
            }
            else
            {
                Sitecore.Data.Database database = Context.Database;
                if (database == null)
                {
                    Tracer.Warning("There is no context in the AliasResolver.");
                }
                else
                { 


                    {
                        Profiler.StartOperation("Resolve virgin alias pipeline.");
                        Item item = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", args.LocalPath, '/'), Language.Current, Sitecore.Data.Version.First, database, SecurityCheck.Disable);
                        if (item != null)
                        {
                            //Alias existis (now we have the alias item)
                            if (item.Fields["Regular Expressions"] != null)
                            {
                                if (!String.IsNullOrEmpty(item.Fields["Regular Expressions"].Value) && !args.Url.QueryString.Contains("aproc"))
                                {
                                   var reg = new Regex(@"(?<Begin>([^/]*/){2})[^/]*/(?<End>.*)");
                                   var match = reg.Match(@"exemple.com/en/solution/platform-features");
                                   var result = match.Groups["Begin"].Value + match.Groups["End"].Value;
                                }
                            }
                        }
                        Profiler.EndOperation();
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Had a problem in the VirginAliasResolver. Error: " + ex.Message, this);
                    }
                }
            }
        }
        ///<summary>
        ///  Once a match is found and we have a Sitecore Item, we can send the 301 response.
        ///</summary>
        private static void SendResponse(string redirectToUrl, HttpRequestArgs args)
        {
            args.Context.Response.Status = "301 Moved Permanently";
            args.Context.Response.StatusCode = 301;
            args.Context.Response.AddHeader("Location", redirectToUrl);
            args.Context.Response.End();
        }
    }
}

PS:我知道他们有这方面的模块,但我的雇主希望这样做,我正在寻求帮助,因为我试图添加这个功能已经一周了

【问题讨论】:

  • 我真的需要帮助
  • 你想达到什么目的?您是否希望找到带有正则表达式的别名(例如 example.com/en/solutions/*)并将其(误用)用于 301?我不明白您为什么要根据路径选择别名,然后执行额外的正则表达式检查。
  • 我只做我老板想要的 这是我第一次在 .NET 和 sitecore 中开发,我不知道该怎么做,我只希望别名有一个单行文本字段,我可以将此正则表达式字符串 /([^/]+)(?=/[^/]+/?$) 与别名放在一起,并能够从那里进行 301 重定向,并且字符串可以知道它是否是正则表达式为什么我选择复选框。
  • 我只是想寻求帮助,因为我自己尝试过但失败了。我什至给自己上了一门复数视觉课程,但这是进阶的方法
  • 我正在努力帮助你,因此我在问你想要实现什么。您是否希望所有以 example.com/en/solutions 开头的 url 都重定向到 Sitecore 中的某个项目?我可以给你一个正确的方向,但考虑到我不清楚你在尝试什么的信息。

标签: c# regex redirect sitecore sitecore7


【解决方案1】:

所以如果我理解正确,您不想通过路径选择别名:

Item item = ItemManager.GetItem(FileUtil.MakePath("/sitecore/system/aliases", args.LocalPath, '/'), Language.Current, Sitecore.Data.Version.First, database, SecurityCheck.Disable);

而是找到一个将正则表达式字段与 URL 进行比较的别名。我没有对此进行测试,但可能类似于:

var originalUrl = HttpContext.Current.Request.Url;
var allAliases = Sitecore.Context.Database.SelectItems("/sitecore/system/aliases//*");
var foundAlias = allAliases.FirstOrDefault( alias =>
                        !string.IsNullOrEmpty(alias["Regular Expressions"]) &&
                        Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), alias["Regular Expressions"]));

然后,如果 foundAlias != null,您可以像在私有 SendResponse 函数中一样检索 url 并重定向。

var linkField = (LinkField)foundAlias.Fields["linked item"];
var targetUrl = linkField.Url;
using (new SecurityDisabler()) 
{
    if (string.IsNullOrEmpty(targetUrl) && linkField.TargetItem != null)
       targetUrl = LinkManager.GetItemUrl(linkField.TargetItem);
}

SendResponse(targetUrl, args);

再说一次,我没有对此进行测试,所以如果需要更正,请不要向我开枪,但这应该可以帮助您继续前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2011-06-08
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多