简体   繁体   中英

Redirect in a Sitecore solution Noob

Here What I am trying to do, my employer want to be able to be able do 301 redirect with regex expression with the alias in Sitecore so the way I am trying to implement this is like this!

a singleline text field with a checkbox to tell sitecore it will be a regex expression I am a noob in .NET and Sitecore how can I implement this ? here a exemple http://postimg.org/image/lwr524hkn/

I need help the exemple of redirect I want handle is like this, this is a exemple of the redirect I want to do it could be product at the place of solution.

exemple.com/en/solution/platform-features to

exemple.com/en/platform-features

I base the code from http://www.cmssource.co.uk/blog/2011/December/modifying-sitecore-alias-to-append-custom-query-strings-via-301-redirect this is for query string I want to use regex expression.

    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: I know they have module for this but my employer want it done that way and I am reaching for help since it's been a week I'm trying to add this feature

So if I understand correctly, you do not want to select an Alias by path:

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

But rather find an Alias comparing a Regex field to the Url. I have not tested this, but it could be someting like:

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"]));

Then, if foundAlias != null, you can retrieve the url and redirect like you do in your private SendResponse function.

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);

Again, I have not tested this so don't shoot me if it needs some corrections, but this should help you get on your way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM