Need users redirected to enforce SSL

N

na

Hello everyone,

I have the following code which works very well:

<script language="javascript" type="text/javascript" >

// This is the hostname that will trigger enforcement of redirection.
var enableRedirect = true;

// This is the hostname that will trigger enforcement of redirection.
var externalHostname = "www.exelonenergy.com";

// This is the reconstructed URL if the current URL contains the
externalHostname:
var redirectURL = "https://" + externalHostname + "/" +
window.location.pathname;

// If the URL contains the externalHostname specified above,
// and the protocol in plain http:, then redirect to the https:
equivalent:

if (enableRedirect == true)
{
if (window.location.hostname.indexOf(externalHostname) >= 0)
{
if (window.location.protocol == "http:")
{
window.location.href = redirectURL;
}
}
}
</script>

What I need though, is the user to be directed if they type in the URL
without the www.

Any help would be great, thanks
 
J

JR

Hello everyone,

I have the following code which works very well:

<script language="javascript" type="text/javascript" >

// This is the hostname that will trigger enforcement of redirection.
var enableRedirect = true;

// This is the hostname that will trigger enforcement of redirection.
var externalHostname = "www.exelonenergy.com";

// This is the reconstructed URL if the current URL contains the
externalHostname:
var redirectURL = "https://" + externalHostname + "/" +
window.location.pathname;

// If the URL contains the externalHostname specified above,
// and the protocol in plain http:, then redirect to the https:
equivalent:

if (enableRedirect == true)
{
        if (window.location.hostname.indexOf(externalHostname) >= 0)
        {
                if (window.location.protocol == "http:")
                {
                        window.location.href = redirectURL;
                }
        }}

</script>

What I need though, is the user to be directed if they type in the URL
without the www.

Any help would be great, thanks

Hi,
I wouldn't use Javascript code to redirect users, because Javascript
could be disabled in the user's browser. That's why URL redirection is
a typical task for server-side script.

E.g in ASP

'The following three code lines are used to ensure that this page is
not cached on the client.
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

If Request.ServerVariables("HTTPS") = "off" Then
Dim varQS, strURL
varQS = Request.ServerVariables("QUERY_STRING")
strURL = Request.ServerVariables("URL")
If Len(varQS &"") > 0 Then strURL = strURL &"?" &varQS
Response.Redirect "https://ssl1003.secureSite.com/" &strURL
Response.End
End If

Cheers,
JR
 
N

na

Hi,
I wouldn't use Javascript code to redirect users, because Javascript
could be disabled in the user's browser. That's why URL redirection is
a typical task for server-side script.

E.g in ASP

'The following three code lines are used to ensure that this page is
not cached on the client.
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

If Request.ServerVariables("HTTPS") = "off" Then
  Dim varQS, strURL
  varQS = Request.ServerVariables("QUERY_STRING")
  strURL = Request.ServerVariables("URL")
  If Len(varQS &"") > 0 Then strURL = strURL &"?" &varQS
    Response.Redirect "https://ssl1003.secureSite.com/" &strURL
    Response.End
  End If

Cheers,
JR- Hide quoted text -

- Show quoted text -

Unfortunately I am using WSS 3.0, so i can only edit the edites source
code with javascript....
 
T

Thomas 'PointedEars' Lahn

na said:
<script language="javascript" type="text/javascript" >

` language="javascript"' is superfluous and potentially invalid.
// This is the hostname that will trigger enforcement of redirection.
var enableRedirect = true;

// This is the hostname that will trigger enforcement of redirection.
var externalHostname = "www.exelonenergy.com";

// This is the reconstructed URL if the current URL contains the
externalHostname:
var redirectURL = "https://" + externalHostname + "/" +
window.location.pathname;

// If the URL contains the externalHostname specified above,
// and the protocol in plain http:, then redirect to the https:
equivalent:

if (enableRedirect == true)

` == true' is superfluous. The expression will be converted to Boolean in
any case, and `enableRedirect' already stores `true', a Boolean value, here.
{
if (window.location.hostname.indexOf(externalHostname) >= 0)

It would be sufficient and probably more efficient to use `> -1'.
{
if (window.location.protocol == "http:")
{
window.location.href = redirectURL;
}
}
}

Try to reduce the number of `if' statements (you only need one here) by
using the `&&' operator.
</script>

What I need though, is the user to be directed if they type in the URL
without the www.

Given that the domains by which this code can be accessed is known, it
should be as simple as removing the `www.' from the string literal.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,084
Messages
2,570,596
Members
47,217
Latest member
GuadalupeE

Latest Threads

Top