c# - Redirect in a Sitecore solution Noob -
here trying do, employer want able able 301 redirect regex expression alias in sitecore way trying implement this!
a singleline text field checkbox tell sitecore regex expression noob in .net , sitecore how can implement ? here exemple http://postimg.org/image/lwr524hkn/
i need exemple of redirect want handle this, exemple of redirect want product @ place of solution.
exemple.com/en/solution/platform-features to
exemple.com/en/platform-features
i base code http://www.cmssource.co.uk/blog/2011/december/modifying-sitecore-alias-to-append-custom-query-strings-via-301-redirect query string want use regex expression.
namespace helloworld.website.sc.common { public class aliasresolver : sitecore.pipelines.httprequest.aliasresolver { // beginning of methods public new void process(httprequestargs args) { assert.argumentnotnull(args, "args"); if (!settings.aliasesactive) { tracer.warning("aliases in aliasresolver not active."); } else { sitecore.data.database database = context.database; if (database == null) { tracer.warning("there no context in 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 have 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 problem in virginaliasresolver. error: " + ex.message, this); } } } } ///<summary> /// once match found , have sitecore item, can send 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: know have module employer want done way , reaching since it's been week i'm trying add feature
so if understand correctly, not want select alias path:
item item = itemmanager.getitem(fileutil.makepath("/sitecore/system/aliases", args.localpath, '/'), language.current, sitecore.data.version.first, database, securitycheck.disable);
but rather find alias comparing regex field url. have not tested this, 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, can retrieve url , redirect in 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, have not tested don't shoot me if needs corrections, should on way.
Comments
Post a Comment