1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Dynamic 301 redirect based on url.

Discussion in 'C#' started by rumblepup, Feb 7, 2007.

  1. #1
    Hello all. I've been trying to figure out a redirect system for a site that I'm updating from classic asp to asp.net.

    Currently, the site has some product search and product detail pages such as:
    prodList.asp?idCategory=9
    or
    prodView.asp?idproduct=55

    Well, what I want to do is create a 301 redirect from those pages to the new one, based on the idCategory parameter or the idproduct parameter.

    I found neat little 301 redirect code

    <%@ Language=VBScript %>
    <%
    Response.Status="301 Moved Permanently" Response.AddHeader "Location", " http://www.new-url.com"
    >

    But I want to create a series of IF/Then statements that will redirect based on the uri parameter, and I'm not sure how to do it.

    In other words, my logic is -

    If WEBSITE uri = ../prodView.asp?idproduct=55
    THEN
    Response.Status="301 Moved Permanently" Response.AddHeader "Location", "new-url(dot)com"

    I have to do this one line at a time because the Product ID's and the Category ID's will change with the new asp.net application.

    In any case, any ideas? I've never written code from scratch, that's why I stink at this.

    Thanks ahead of time.
     
    rumblepup, Feb 7, 2007 IP
  2. tgo

    tgo Peon

    Messages:
    124
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like this...
    I personaly would use a DB to pull the new product page from if you can instead of hard coding it.
    
    <%
    varHost = Request.ServerVariables("HTTP_HOST")
    varPage = Request.ServerVariables("SCRIPT_NAME")
    varQuery = Request.ServerVariables("QUERY_STRING")
    If varHost = "yoursite.com" then
    Response.Status="301 Moved Permanently"
      If varPage = "/prodList.asp" then
        If varQuery = "idCategory=1"
          Response.AddHeader "Location", "new_location.asp"
        Elseif varQuery = "idCategory=2"
          Response.AddHeader "Location", "new_location.asp"
        Else
          Response.AddHeader "Location", "new_location.asp"
        End If
      Elseif varPage = "/prodView.asp"
        If varQuery = "idproduct=1"
          Response.AddHeader "Location", "new_location.asp"
        Elseif varQuery = "idproduct=2"
          Response.AddHeader "Location", "new_location.asp"
        Else
          Response.AddHeader "Location", "new_location.asp"
        End If
      Else
        Response.AddHeader "Location", "unknown_list.asp"
      End If
    End If
    %>
    
    Code (markup):
    Hope this helps
     
    tgo, Feb 7, 2007 IP
  3. rumblepup

    rumblepup Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    COOL BEANS!!!

    Thank you so much. You have no idea how many people I've asked. Seems everybody has forgotten asp.

    I would love to, but like I said in my post, can't code if my life depended on it. I can script scrape like a pro, putting bits and pieces together to make it do what I want it to do, but otherwise, bupkis.

    Does this work for all versions of the url? In other words, http://www and http:// and https:// ? This is the problem I've got, both www and non www AND https pages are currently listed.

    Thanks.
     
    rumblepup, Feb 8, 2007 IP
  4. tgo

    tgo Peon

    Messages:
    124
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    red is added code
    green is comments
    <%
    varHost = Request.ServerVariables("HTTP_HOST")
    varPage = Request.ServerVariables("SCRIPT_NAME")
    varQuery = Request.ServerVariables("QUERY_STRING")
    [COLOR="DarkGreen"]'i dont think this is needed, but if so you can if this... too.[/COLOR]
    [COLOR="Red"]varSecure = Request.ServerVariables("HTTPS")[/COLOR]
    
    If varHost = "yoursite.com"[COLOR="Red"] OR "www.yoursite.com"[/COLOR] then
    Response.Status="301 Moved Permanently"
      If varPage = "/prodList.asp" then
        If varQuery = "idCategory=1"
          Response.AddHeader "Location", "new_location.asp"
        Elseif varQuery = "idCategory=2"
          Response.AddHeader "Location", "new_location.asp"
        Else
          Response.AddHeader "Location", "new_location.asp"
        End If
      Elseif varPage = "/prodView.asp"
        If varQuery = "idproduct=1"
          Response.AddHeader "Location", "new_location.asp"
        Elseif varQuery = "idproduct=2"
          Response.AddHeader "Location", "new_location.asp"
        Else
          Response.AddHeader "Location", "new_location.asp"
        End If
      Else
        Response.AddHeader "Location", "unknown_list.asp"
      End If
    End If
    %>
    Code (markup):
    Secure pages should work as normal as the SCRIPT_NAME is the same in both cases, i added a check for WWW version as the old one didnt check.
     
    tgo, Feb 8, 2007 IP
  5. rumblepup

    rumblepup Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    :D Your a beast my friend. Thank you very much.
     
    rumblepup, Feb 9, 2007 IP
  6. user65

    user65 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi - First I am a noob at this, so please don't laugh at my question.

    Where would I insert this? on the header.asp file? I am going to try, but any help would be appreciated.

    Thank you,
     
    user65, May 21, 2010 IP
  7. omerlevy

    omerlevy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    HI all.
    i want to redirect with 301 for a different folder.


    the folder name is a variable and its different every time


    so what i get is somthing like that:


    Response.Status="301 Moved Permanently"
    Response.AddHeader "Location","http://www.mySiteName.co.il/" MyFolderName


    that is not working for sure !


    how can i join the folder name to the redirect statement?


    tnx!
     
    omerlevy, Dec 10, 2012 IP