Search code examples
asp.net.nethttpsiis-6

Redirecting the whole site to HTTPS in ASP.net application


I have done a project using asp.net and is ready to go live. Now the management has decided to use SSL for some reasons and hence the site should load in HTTPS.

Since the project is quite big, and it will be painful to check the URL and redirect it to HTTPS in all the pages, Is there a simple way where I can handle this globally or in my master pages ?

So, even if the website URL is typed with HTTP, it should be redirected to HTTPS site. How can I achieve this ?


Solution

  • If you don't want to control it per IIS like Nate suggested, you can simply do the redirection in your global.asax:

    void Application_BeginRequest(Object sender, EventArgs e)
    {
        if (Request.Url.Scheme != "https")
            Response.Redirect("https://yoursite.com");
    }
    

    May not be the most witty solution, but it does the job.