Link to home
Start Free TrialLog in
Avatar of Nabsdar
Nabsdar

asked on

Redirect to HTTPS

Hello,

How can I configure IIS 5 to redirect a website (virtual directory) to a secured site? When users type http://www.website.com/website they get an error and I have to tell them to insert a 's' in after the http(s) to make it viewable. Basically I want to configure IIS to automatically redirect them when they type http://www.website.com/website to https://www.website.com/website? Can someone please assist me?

Thanks in advance

Nabs
Avatar of daniel_c
daniel_c

Add small snippet in your default.asp

<%
  bSSLOn = (Request.ServerVariables("HTTPS") = "on")
  if not bSSLOn then
    sURL = "https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")
    if Request.ServerVariables("QUERY_STRING") <> "" then
      sURL = sURL & "?" & Request.ServerVariables("QUERY_STRING")
    end if
    Response.Redirect sURL
  end if
%>

HTH.
Avatar of Nabsdar

ASKER

Thanks for your prompt input but is this not going to effect the entire website? I am just looking for redirection on a virtual directory http://www.website.com/virtual_directory.com to an https. Please advice

Thanks again,
Nabs


Other things might be set your virtual home directory properties, instead to specific folder, make it to specific url:
https://www.website.com/virtual_directory

Avatar of Nabsdar

ASKER

This solution is not working very well for me! Any other ideas anyone?

Thanks
Nabs
Simpler solution:
1. Create one html page, name it redirectssl.htm and save it into your virtual directory folder. Here is the redirectssl.htm content:
<script language=javascript>
      self.location = "https://www.website.com/virtual_directory.com "
</script>
<noscript>
<META HTTP-EQUIV=Refresh CONTENT="1; URL=https://www.website.com/virtual_directory.com ">
</noscript>

2. Open your IIS, right click on your virtual directory site, click Properties.
3. Switch to Custom Error Tab
4. Edit properties of 403;4.
5. Set message type to FILE
6. Click browse button and point it into your redirectssl.htm
7. Click OK button twice.

Now try to put http://www.website.com/virtual_directory.com  (without s).

HTH.
Avatar of Shalom Carmel
You need something similar to Apache's Rewrite....

http://www.isapirewrite.com/

This tool will let your server redirect URLs based on various criteria.

ShalomC
Avatar of Nabsdar

ASKER

Hello daniel_c

You are definitely on the right track with the last solution to my problem but it still does not work! Please advice further.

Thanks for your time,
Nabs
it does not work? you mean it doesn't redirect?
Avatar of Nabsdar

ASKER

Yes, it does not redirect.
Post your HTML file code. I need to see it.
Avatar of Nabsdar

ASKER

Okay it worked!
I had put in the wrong code!

Thanks daniel_c: so now to apply this to our current directory, should I just insert this code in it or how do I go about this?

I really appreciate your help
Nabs

Avatar of Nabsdar

ASKER

One other problem:

I don't see the contents of the page. It redirects it to https but its just a page with an error!
Why?
ASKER CERTIFIED SOLUTION
Avatar of daniel_c
daniel_c

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Each website has a default document, the redirect should be named that (I.E. default.htm)

The target website has a document you want people to load, so specify it in the code.

---
script language=javascript>
     self.location = "https://www.website.com/virtual_directory/HTMLfileNAME.HTM "
</script>
---
Avatar of Nabsdar

ASKER

Thanks daniel_c fir putting me on the right direction. I finally got it to work perfectly!!

Nabs

Great!!!
We use an SSL accerlerator so ASP doesn't see the request as HTTPS.  I came up with the code below to perform the redirect using JavaScript since the client always sees the request as SSL.

<SCRIPT LANGUAGE="JavaScript">
if (location.protocol != 'https:'){
      window.location= 'https://' + location.host + location.pathname + location.search
}
</SCRIPT>
Just wanted to post my solution.
Basicaly same as above by Daniel

1. Open your IIS, right click on your virtual directory site, click Properties.
2. Switch to Custom Error Tab
3. Edit properties of 403;4.
4. Set message type to FILE
5. Click browse button and point it into your redirect.htm
6. Click OK button twice.


redirect.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>API E-MAIL SERVER</title>
<meta http-equiv="REFRESH" content="1;url=https://site/exchange"></HEAD>
<BODY>
Loading... https://site/exchange
</BODY>
</HTML>

note:
content="1  - is the time in seconds it will take to redirect
Another (and perhaps better) way is to have two websites - one that accepts host headers for mysite.com, .org, .net and whatever else you want. Everything is on port 80. In the Home Directory tab in IIS manager, set it to "A redirection to a URL". Enter https://mysite.com. Also check the permanent redirect box.
Next, in the site you want to force SSL, make sure it also responds to the mysite.com host header, but change the port to something high - make sure its unreachable behind your firewall. Set the security as necessary and you're done.
I have to agree with dmbrubac (for IIS, anyway). Changing the 403;4 error page to a redirect will send you to the the same page every time. If you have hyperlinks from other websites that point to subdirectories within the domain, the hyperlinks will not go where expected, instead they will always point to the URL in the redirect--forcing the unsuspecting user to re-navigate to the desired page once they arrive.

With two websites (on IIS), you can set the redirect from the port 80 site to https://mysite.com$S$Q, which will act as expected and send the user to the desired subdirectory every time.