27 May 2011

Redirect scripts to change the default landing location

By default a when an IIS site is setup the landing page is iisstart.htm.  To change this there are a few things you can do.


The Default Document contains a list of files it will try, in order till one works.

  • Default.htm
  • Default.asp
  • Index.htm
  • Index.html
  • iisStart.htm

If any of those are present they will be loaded instead of iisstart.  The easiest and simplest is just to set the Default Document to the new file.

If however you application installs to a virtual directory you cannot simply change the default document to be one that is on the virtual directory.  You will have to redirect to it.  There are many ways of doing this but I will go through two of them, both using my "disturb as little as possible" principle.

Method 1 - Edit the iisstart.htm file

Dropping a client side script into this file will redirect the user on page load to the desired page.

add the following in the <head> section


<script language="JavaScript">
var time = null
function move() {
window.location = 'http://www.yourdomain.com/folder/page.asp'
}
//-->
</script>

add the following in the <body> section

<body onload="timer=setTimeout('move()',0)">

What happens is that when the page is loaded the browser will then execute the script that will load up the new page. So two pages are loaded.

Method 2 - Use the default.asp file

By using the asp file we can make use of a server side script to do the redirect.  Create a file and save it as default.asp in the c:\inetpub\wwwroot folder.  Copy the following into it.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" 
Response.AddHeader "Location", "http://www.yourdomain.com/folder/page.asp"
response.end
%>
</body>
</html>

When the page is requested the server parses the script and only sends the 1 page instead of the original landing and then the redirected page as per the client side method.

Both examples will work in most cases but you might only be able to use one of them depending on whether serverside scripting or client side scripting is allowed.

Method 3 - Use Custom Error Pages

By default when a page is requested from a server that does not exist you will receive the 404 error page.  With IIS you can use custom error pages, or you can have re director as an alternative to displaying the error page.  A nice touch here is that you can have a relative or absolute redirect.

Select your site and click on the Error Pages icon

Select 404 from status code column and edit

Select "Respond with a 302 redirect" and specify the full URL you want to redirect to.
The alternative here is also to edit the actual error page, you can for instance include the javascript in the error page as opposed to the iisstart page.

The actual error pages are located in C:\inetpub\custerr\en-US\

No comments:

Post a Comment