2

To handle 301 redirect we have made changes in pipeline and written following class

public class CustomRedirect301 : PreprocessRequestProcessor
{
    public override void Process(PreprocessRequestArgs args)
    {
        var targeturl = objDB.GetTargetURL(args.Context.Request.Url.PathAndQuery.TrimEnd(new[] { '/' });)
        args.Context.Response.RedirectPermanent(targeturl, true);
    }
}

In GetTargetURL function we pass incoming URL part only after domain for example incoming request is www.mysite.com/healthcare we pass /healthcare and this function return actual url where user should redirect let's say www.mysite.com/part-1/part-2/healthcare

We maintain this mapping in database table with two column UrlFrom and UrlTo so in this case

UrlFrom is /healthcare

UrlTo is www.mysite.com/part-1/part-2/healthcare

But now we have one request where we have to redirect abc.mysite.com to www.mysite.com/part-1/part-2/part-3

In this case we don't have any URL part after domain to pass as input parameter in GetTargetURL function so how can we achieve this.

Environment: Sitecore 8.1 MVC / Multisite implementation

  • Assuming that this is a single case and you don't need to be able to edit this frequently, you can use url rewrite module. – Marek Musielak Mar 15 '17 at 21:54
  • in place of rewrite module we implemented this so we are not using url rewrite module anymore now that's why we are looking for other way – Sukhjeevan Mar 16 '17 at 0:38
  • Have you considered using aliases? – Dmytro Shevchenko Mar 16 '17 at 7:38
1

If you don't want to use URL Rewrite Module (and from your comment I see you don't want it), you need to change the code of your processor slightly.

Add an entry like

UrlFrom ://abc.mysite.com
UrlTo www.mysite.com/part-1/part-2/part-3

and call GetTargetURL as you do now:

var key = args.Context.Request.Url.PathAndQuery.TrimEnd(new[] { '/' });

// for "abc.mysite.com" key will be an empty string
if (string.IsNullOrEmpty(key))
{
    // for "abc.mysite.com" it will return "://abc.mysite.com"
    key = string.Format("://" + Request.Url.DnsSafeHost);
}
var targeturl = objDB.GetTargetURL(key);

If you do this like that, you don't need to write any custom code that will check if domain is abc.mysite.com. And you will be able to add more subdomain redirects without any code changes.

One important thing is: you need to make should that either your database call are really efficient or cached. You don't want to make a call to database for every home page request if it's not necessary.

  • yeah I think for particular this case first I have to check the value of args.Context.Request.Url . If it's abc.mysite.com then I will pass args.Context.Request.Url in GetTargetURL() function. In this function we will check if UrlFrom entry and will return UrlTo value...Is it correct? – Sukhjeevan Mar 16 '17 at 20:17
  • Answer updated. More code with working solution added. – Marek Musielak Mar 16 '17 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.