Click here to Skip to main content
15,893,923 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

My project is in MVC C# and sitecore. I need to implement 301 redirect if url doesn't contain https://www.mysite.com. If URL doesn't contain this domain url then I need to redirect with secure and www url. It should show in console. How can I do it. I have done it using jquery but it is not showing 301 redirect message in console of browser.


Please help me.

What I have tried:

Global.asax is making side slow and redirect every link of site on http so it is not showing images of site.
jquery working fine but not showing 301 message.
Posted
Updated 24-May-16 23:43pm

1 solution

Never do anything security related using javascript. Also why do you want to see something in the console? Anyway, you normally handle this using server-side code. Want you want to do is modify the httpRequestBegin pipeline and inject your own step. Create a class called HttpsRedirector

C#
using Sitecore.Pipelines.HttpRequest;

namespace YourNamespace.httpRequestBegin
{
    public class HttpsRedirector : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            // might want to add some logic here to ignore certain urls

            if (!args.Context.Request.IsSecureConnection)
            {
                string url = string.Format("https://{0}{1}", args.Context.Request.Url.Authority, args.Context.Request.RawUrl);

                Sitecore.Diagnostics.Log.Info(string.Format("Redirecting to secure connection for '{0}'", url), this);
                args.Context.Response.RedirectPermanent(url);
                args.AbortPipeline();
            }
        }
    }
}


Create a config file in the "App_Config/Include" folder called httpRequestBegin.config (doesn't really matter what you call it)

XML
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="YourNamespace.HttpsRedirector, YourAssemblyName" patch:before="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']"/>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900