+1-888-365-2779
Try Now
More in this section

Forums / Deployment / ensuring www prefix

ensuring www prefix

12 posts, 0 answered
  1. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    04 Aug 2008
    Link to this post
    the ssl certificate for our site is setup to include the www prefix, like www.site.com. if a user visits site.com, they will see the same site and everythign works fine until they get to a secured page. since the ssl is for www.site.com, and they are visiting site.com, they see an invalid certificate error. Is there a way to ensure that anyone who enters site.com will be redirected to www.site.com? bear in mind we do have subdomains (such as sub.site.com) that need to be unaffected by any changes.

    thank you!
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    05 Aug 2008
    Link to this post
    Hi SelArom,

    The easiest solution to this problem is to use Http 301 IIS Redirects or to create a HttpHandler that makes the redirect. If you choose the http-handlers approach, you will have to declare the new httphandler before ours in the web.config file.

    Regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Ed Hardy
    Ed Hardy avatar
    17 posts
    Registered:
    28 Aug 2012
    05 Aug 2008
    Link to this post
    Hi guys,

    I think that "Microsoft URL Rewrite Module" can also be used as a solution.

    Hope this helps

  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    06 Aug 2008
    Link to this post
    thanks for the replies... the module referenced above is for IIS 7 only, no? that's all I've been able to find. the 301 IIS redirects linked above seems overly complicated...

    I've written httphandlers before but I don't think I know enough about them to plug something into reroute all requests! how do other people usually handle this? the ssl problem i mean.
  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    08 Aug 2008
    Link to this post
    Hello SelArom,

    Your module could only redirect the requests to your domain starting with www (in other words, adding the prefix). The rest of the work (switching to https://) will be automatically handled by our HttpModule, if your home page is SSL enabled through the project Administration panel.

    Best wishes,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    11 Aug 2008
    Link to this post
    I did notice that you have a module that does this automatically, but when I meant how is this usually handled I mean how do I redirect from non www to the www. prefix? do I just inspect the full url and if it doesn't start with http://www then I replace it and redirect?
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    14 Aug 2008
    Link to this post
    Hello SelArom,

    Attached is a HttpModule that will handle the redirection. It should be placed in the App_Code directory, and defined in the web.config like this:
        <httpModules> 
          .. 
          <add name="Custom" type="Telerik.Cms.WwwRedirect.CustomModule"/> 
          <add name="Cms" type="Telerik.Cms.Web.CmsHttpModule, Telerik.Cms" /> 
          <add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule, Telerik.Web.UI" /> 
        </httpModules> 
    Please note that it is defined before ours. At a later stage, our CmsHttpModule will handle the secured connections and switching from http:// to https:// and vice versa.

    There is another approach also. You could create a new virtual directory, and redirect all requests to it to the other virtual directory, starting with www. This way, the request processing will not reach the ASP.NET engine but will be handled by IIS.

    I hope this helps.

    Kind regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. AntonioWells.com
    AntonioWells.com avatar
    14 posts
    Registered:
    21 Oct 2008
    24 Aug 2009
    Link to this post
    I tried this module and got a Error Code 400 "Bad Request", and the URL in the Address Bar was this:
    http://www.axa-assistance.com.pa/:%20www.http://www.axa-assistance.com.pa/Default.aspx

    Does this work with version 3.6 SP1?
  9. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    24 Aug 2009
    Link to this post
    I ended up not using this module, but made my own to check the full url, and if the www is not present in the host, append it and perform a 301 redirect. below is a simplified snippet of what I'm doing. any suggestions for improvement would be appreciated!

    // get requested page & full URL  
    var rawUrl = Context.Request.RawUrl;  
    var host = new Uri(Context.Request.Url.ToString()).Host;  
     
    if (host != "www.mydomain.net")  
    {  
        var newUrl = "http://www.mydomain.net";  
     
        // append only if not root page  
        if (rawUrlToLower != "/default.aspx")  
            newUrl += rawUrlToLower;  
     
        Context.Response.Clear();  
        Context.Response.Status = "301 Moved Permanently";  
        Context.Response.AddHeader("Location", newUrl);  
        Context.Response.End();  
  10. Gavin
    Gavin avatar
    20 posts
    Registered:
    06 Jul 2009
    16 Oct 2009
    Link to this post
    Thanks for the great starting point! I wanted to use something like this for SEO purposes. I've adapted the code to work with any domain and also to cope with a localhost dev environment.

    Here it is for anyone else that might benefit...

    using System; 
    using System.Web; 
     
    namespace GenericLib.Cms 
        /// <summary> 
        /// Ensures url always uses www at the beginning of the domain. 
        /// </summary> 
        public class WwwRedirect : IHttpModule 
        { 
            public void Dispose() 
            { 
            } 
     
            public void Init(HttpApplication context) 
            { 
                context.BeginRequest += new EventHandler(context_BeginRequest); 
            } 
     
            void context_BeginRequest(object sender, EventArgs e) 
            { 
                HttpContext context = HttpContext.Current; 
                HttpRequest request = context.Request; 
     
                if (!request.Url.AbsoluteUri.StartsWith(request.Url.Scheme + "://www.") && !request.Url.AbsoluteUri.StartsWith(request.Url.Scheme + "://localhost")) 
                { 
                    context.Response.RedirectLocation = request.Url.AbsoluteUri.Replace(request.Url.Scheme + "://", request.Url.Scheme + "://www."); 
                    context.Response.StatusCode = 301; 
                    context.Response.End(); 
                } 
            } 
        } 

  11. Aaron
    Aaron avatar
    24 posts
    Registered:
    15 Feb 2008
    23 Oct 2009
    Link to this post
    Gavin,

    Thanks for posting your code.  I am in need of this exact solution.  Where did you place this code in your website?  I am assuming in the /bin directory.  Did you change any of the web.config file sections to make it work?

    Thanks in advance.
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    23 Oct 2009
    Link to this post
    Hi Christopher,

    You can add the code in your App_Code folder. If you compile it then you will have a dll to put in the bin folder.
    You need to register the module in <httpModules> section , or under <system.webServer><modules> if you use IIS integrated pipline mode.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
12 posts, 0 answered