简体   繁体   中英

How to redirect non www to www in sitecore?

Apart from installing rewrite modules is there a simple way to redirect non-www to www in Sitecore?

Any URL started with non-www for Eg: http://example.com should be redirected to http://www.example.com

You can add custom processor to the httpRequestBegin pipeline if you want to keep everything in your .net code.

But still, rewrite module is one of the most popular ways of achieving what you need.

This kind of things you want to do as early as possible. On application level it will unnecessarily use resources. Try IIS or even better through DNS redirects.

 if (!Request.Url.Host.StartsWith("www") && !Request.Url.IsLoopback)
 {
    var builder = new UriBuilder(Request.Url) { Host = "www." + Request.Url.Host };
    Response.StatusCode = 301;
    Response.AddHeader("Location", builder.ToString());
    Response.End();
 }

The proper way to do it would be to set the targethostname in a Site definition config patch file - although if you've got a single site instance of Sitecore you may just do it in the web.config.

The way to do it is to set hostname="example.com|www.example.com" and then set targethostname="www.example.com".

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