The following code assumes that if the server name doesn't start with a "www." then the remedy is to prepend whatever the current servername is with "www."
if (!Request.IsSecureConnection)
{
// send user to SSL
string serverName = Request.ServerVariables["SERVER_NAME"];
if (!serverName.ToLowerCaseInvariant().StartsWith("www.")) {
serverName = string.Format("www.{0}", serverName);
}
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);
}
Personally, I don't like this method of doing things. I usually create a setting named something like SecureDomain
and then use logic to verify whether the current ServerName matches that. Something like this.
// Suppose the value of GlobalAppSettings.SecureDomain
// is something like www.securestore.com
if (!Request.IsSecureConnection)
{
// send user to SSL
string serverName = Request.ServerVariables["SERVER_NAME"];
if (string.Compare(serverName, GlobalAppSettings.SecureDomain, true) != 0) {
serverName = GlobalAppSettings.SecureDomain;
}
string filePath = Request.FilePath;
Response.Redirect("https://" + serverName + filePath);
}