I need help with 301 redirect in Microsoft Server

Discussion in 'Site & Server Administration' started by Basoone, Mar 21, 2007.

  1. #1
    Basoone, Mar 21, 2007 IP
  2. tgo

    tgo Peon

    Messages:
    124
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make a file and include it in the very top of every page.

    Paste this code in the included file
    <%
    If Request.ServerVariables("HTTP_HOST") = "www.domain.com" then
    Response.Status="301 Moved Permanently"
    	If Request.ServerVariables("SCRIPT_NAME") = "/Index.asp" then
    		Response.AddHeader "Location", "http://domain.com"
    	Else
    		Response.AddHeader "Location", "http://domain.com" & Request.ServerVariables("SCRIPT_NAME")
    	End If
    End If
    %>
    Code (markup):
     
    tgo, Mar 21, 2007 IP
  3. Basoone

    Basoone Peon

    Messages:
    544
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What extension should I use?

    Many Thanks
     
    Basoone, Mar 21, 2007 IP
  4. oziman

    oziman Active Member

    Messages:
    199
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Use .asp extension.
     
    oziman, Mar 22, 2007 IP
  5. plumsauce

    plumsauce Peon

    Messages:
    310
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    add this:

    Response.Flush
    Response.End

    just before:

    End If
    %>
     
    plumsauce, Mar 23, 2007 IP
  6. Basoone

    Basoone Peon

    Messages:
    544
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i tried this and its not working:

    
    <%
    If Request.ServerVariables("HTTP_HOST") = "www.dogcontainment-4less.com" then
    Response.Status="301 Moved Permanently"
    	If Request.ServerVariables("SCRIPT_NAME") = "/Index.asp" then
    		Response.AddHeader "Location", "http://dogcontainment-4less.com"
    	Else
    		Response.AddHeader "Location", "http://dogcontainment-4less.com" & Request.ServerVariables("SCRIPT_NAME")
    	End If
    	Response.Flush
    Response.End
    End If
    %>
    
    Code (markup):
    What do I miss?
     
    Basoone, Mar 26, 2007 IP
  7. tgo

    tgo Peon

    Messages:
    124
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <%
    If Request.ServerVariables("HTTP_HOST") = "www.dogcontainment-4less.com" then
    Response.Status="301 Moved Permanently"
    	If Request.ServerVariables("SCRIPT_NAME") = "[COLOR="Red"]/yourmainpage.asp[/COLOR]" then
    		Response.AddHeader "Location", "http://dogcontainment-4less.com"
    	Else
    		Response.AddHeader "Location", "http://dogcontainment-4less.com" & Request.ServerVariables("SCRIPT_NAME")
    	End If
    	Response.Flush
    Response.End
    End If
    %>
    Code (markup):
    other than that it should work.

    What the inner if is doing is checking to see if the user is requesting the index page. In asp index.asp is loaded when loading the domain name, so if you have your main page named anything different then it will make a problem, just use this if its not index.asp...

    <%
    If Request.ServerVariables("HTTP_HOST") = "www.dogcontainment-4less.com" then
       Response.Status="301 Moved Permanently"
       Response.AddHeader "Location", "http://dogcontainment-4less.com" & Request.ServerVariables("SCRIPT_NAME")
       Response.Flush
       Response.End
    End If
    %>
    Code (markup):
     
    tgo, Mar 26, 2007 IP
  8. plumsauce

    plumsauce Peon

    Messages:
    310
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The very first line may need to be Response.Buffer=TRUE

    This is because headers cannot be changed once the response has started unless buffering is on.

    .
     
    plumsauce, Mar 26, 2007 IP