Url Redirection in Classic Asp page

15.8k Views Asked by At

I have a issue related to redirection

I want to apply redirection if someone uses http://mywebsite.com/ then the URL would be redirected to http://www.mywebsite.com/. I know it is a 302 redirection but I dont know how to apply it in coding ........ what VBScript could be used for applying redirection ??

My website is build in Classic ASP and VBScript ... any piece of code would be better for me

Thanks

2

There are 2 best solutions below

2
On BEST ANSWER

Use Request.ServerVariables("HTTP_HOST") to get the host part so that you can check if it starts with www. or not.

If it doesn't then just issue a Response.Redirect() to the appropriate URL since it'll do a 302 for you:

e.g.

If Left(Request.ServerVariables("HTTP_HOST"), 4) <> "www." Then
  Dim newUri
  'Build the redirect URI by prepending http://www. to the actual HTTP_HOST
  'and adding in the URL (i.e. the page the user requested)
  newUri = "http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL")

  'If there were any Querystring arguments pass them through as well
  If Request.ServerVariables("QUERY_STRING") <> "" Then
    newUri = newUri & "?" & Request.ServerVariables("QUERY_STRING")
  End If

  'Finally make the redirect
  Response.Redirect(newUri)
End If

The above does a redirect ensuring the requested page and querystring are preserved

2
On

Try this:

Response.Status = "302 Moved Temporary"
Response.AddHeader "Location", "http://www.mywebsite.com/"