Redirect URLs on Windows Cloud Sites
Use at your own risk
The script provided below is only an example. As outlined in our Terms of Service and our article Support Scope, we are unable to help you troubleshoot code and script issues. We recommend speaking with your developer before implementing any script functionality.
Introduction
In order to allow your users to securely access your website, you must use an SSL. This changes the HyperText Transfer Protocol (http) to HyperText Transfer Protocol Secure (https). Using https helps keep your site visitor information safe from hackers by encrypting the information and making it difficult to translate. If you aren’t familiar with SSL’s check out our article What Is an SSL Certificate?.
Once you’ve purchased your SSL and it is installed on your site, you will need to use redirects for URLs and subdomains to force traffic through the SSL. Alternatively, if you are using Windows on Cloud Sites, you can use one of the methods below to redirect your URLs and subdomains. If you are using Linux on Cloud Sites, follow the instructions here to redirect using your .htaccess file.
Prerequisites
Windows Server Only
The following scripts are for use on a Cloud Sites plan with Windows as its operating system. If you are using Linux on Cloud Sites, follow the instructions here to redirect using your .htaccess file.
- A cloud sites plan using Windows as its operating system.
- A domain with an active SSL certificate installed.
Using web.config
If you are using the IIS Rewrite Module for your site, you can write the script to force SSL on all pages on your site. An example of the script is below.
<? xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_CLUSTER_HTTPS}" pattern="^on quot; negate="true"/>
<add input="{HTTP_CLUSTER_HTTPS}" pattern=".+" negate="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{SCRIPT_NAME}" redirectType="SeeOther"/> </rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
More information on the IIS Rewrite Module
For information about the IIS Rewrite Module, you can review the documentation on the Microsoft IIS site and the articles Creating Rewrite Rules for the URL Rewrite Module and URL Rewrite Module Configuration Reference.
Using ASP.NET
MVC3 & higher
<%@ Page Language="C#" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!script runat="server">// <!
protected void Page_Load(object sender, System.EventArgs e)
{
if(Request.ServerVariables["HTTP_CLUSTER_HTTPS"] != "on")
{
if(Request.ServerVariables.Get("HTTP_CLUSTER_HTTPS") == null)
{
string xredir__, xqstr__;
xredir__ = "https://" + Request.ServerVariables["SERVER_NAME"];
xredir__ += Request.ServerVariables["SCRIPT_NAME"];
xqstr__ = Request.ServerVariables["QUERY_STRING"];
if (xqstr__ != "")
xredir__ = xredir__ + "?" + xqstr__;
Response.Redirect(xredir__);
}
}
Response.Write("SSL Only");
}
</script>
<html>
<head id="head1" runat="server> <title>SSL Only</title> </head>
<body> </body> </html>
MVC 1-2
<%@ Page Language="C#" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!script runat="server">// <!
protected void Page_Load(object sender, System.EventArgs e)
{
if(Request.ServerVariables["HTTP_CLUSTER_HTTPS"] != "on")
{
if(Request.ServerVariables.Get("HTTP_CLUSTER_HTTPS") == null)
{
string xredir__, xqstr__;
xredir__ = "https://" + Request.ServerVariables["SERVER_NAME"];
xredir__ += Request.ServerVariables["SCRIPT_NAME"];
xqstr__ = Request.ServerVariables["QUERY_STRING"];
if (xqstr__ != "")
xredir__ = xredir__ + "?" + xqstr__;
Response.Redirect(xredir__);
}
}
Response.Write("SSL Only");
}
</script>
<html>
<head id="head1" runat="server> <title>SSL Only</title> <body> </body> </html>
Classic ASP
<% Response.Buffer = True
If (Request.ServerVariables("HTTP_CLUSTER_HTTPS") <> "on") Then
If IsEmpty(Request.ServerVariables("HTTP_CLUSTER_HTTPS")) Then
Dim xredir__, xqstr__
xredir__ = "https://" & Request.ServerVariables("SERVER_NAME") & _
Request.ServerVariables("SCRIPT_NAME")
xqstr__ = Request.ServerVariables("QUERY_STRING")
If xqstr__ <> "" Then xredir__ = xredir__ & "?" & xqstr__
Response.Redirect xredir__
End If
End If
Response.Write("SSL Only")
%>
Next Steps
Now that you’ve seen how to redirect URLs to use your SSL, find out how to redirect subdomains in our article Redirecting Subdomains using ASP and ASP.NET in Cloud Sites.