Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    301 redirect from non-www to www - RESOLVED

    I have a file 301.asp with the content (left the '< %' and '% >' out):

    Code:
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
        Response.Status="301 Moved Permanently"
        Response.AddHeader "Location","http://www." & _
            Request.ServerVariables("HTTP_HOST") & _
            Request.ServerVariables("REQUEST_URI") & _
        Response.End
    End if
    Made a test-page aa.asp, that has this content:
    Code:
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
        Response.Status="301 Moved Permanently"
        Response.AddHeader "Location","http://www." & _
            Request.ServerVariables("HTTP_HOST") & _
            Request.ServerVariables("REQUEST_URI") & _
        Response.End
    End if
    Now when I goto http://www.worldofrugs.com/shop/pc/aa.asp it shows fine.

    Going to http://worldofrugs.com/shop/pc/aa.asp it does NOT redirect to the aa.
    asp with the www included, but throws me to the http://www…./index.asp page…

    Anyone any idea why this is happening?
    Last edited by worldofrugs; 01-12-2010 at 07:10 PM. Reason: set status to resolved

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    seems like it should work, but I don't have an asp server to test on.
    try debugging.

    see if this gets hit:
    Code:
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then response.end
    you could also try printing out the full url and make sure that it's getting formatting correctly.

  • #3
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Changed the 301.asp to:

    Code:
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    		Response.Write("OK")
    	Response.End
    End if
    This gives me OK, so I guess it does.
    Last edited by worldofrugs; 01-12-2010 at 05:57 PM.

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm ok, try a response write on this:

    Code:
    response.write "http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("REQUEST_URI")

  • #5
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    This gives me: http://www.worldofrugs.com

    Seems like everything after the domain is somehow not coming over?


    P.s.: Thanks so much for assisting me on this issue.. Really appreciate that

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, i don't see the URI variable in the list: http://www.w3schools.com/asp/coll_servervariables.asp

    maybe try this:
    Code:
    response.write "http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & "?" & Request.ServerVariables("QUERY_STRING")

  • #7
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You the man!!!!


    That works great!

    Made my final code now:
    Code:
    <%
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    	Response.Status="301 Moved Permanently"
    	Response.AddHeader "Location","http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & "?" & Request.ServerVariables("QUERY_STRING")
    	Response.End
    End if
    %>
    1 more question about this...
    Whilr it is working good, is the "?" needed?
    Example:
    http://123.com/aa.asp --becomes--> http://www.123.com/aa.asp?

    Tested it with variables as well:
    http://123.com/aa.asp?idCategory=35 --becomes--> http://www.123.com/aa.asp?idCategory=35

    So it works fine, just wondered if the question mark was needed when no idCategory (for example) was set..

    Again thanks so much for all your help Angst and providing a solution that prevented me from going like this:

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    lol,, no problem.

    what you could do it just test to see if the query string exists, like:

    Code:
    <%
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    	Response.Status="301 Moved Permanently"
    
            QueryString = Request.ServerVariables("QUERY_STRING")
            if QueryString <> "" Then QueryString = "?" & QueryString
    	Response.AddHeader "Location","http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & "?" & QueryStrin
    	Response.End
    End if
    %>

  • Users who have thanked angst for this post:

    worldofrugs (01-12-2010)

  • #9
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    This actually still gives me the "?"
    When using xx.asp?id=1 it results in: www... /xx.asp??id=1

    I'm not a very experienced coder, but would something like this perhaps work?
    Code:
    <%
    Dim ABC
    Dim DEF
    ABC = "?" & Request.ServerVariables("QUERY_STRING")
    DEF = Request.ServerVariables("QUERY_STRING")
    IF DEF.Contains("?") then
    ABC = ABC
    Else
    ABC = Request.ServerVariables("QUERY_STRING")
    End if
    
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    	Response.Status="301 Moved Permanently"
    	Response.AddHeader "Location","http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & ABC
    	Response.End
    End if
    %>

  • #10
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm ok, well maybe you can remove the question mark, as I said I can't test this code and it's been a while since I've used it.

  • Users who have thanked angst for this post:

    worldofrugs (01-12-2010)

  • #11
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    No worries, I'll work it out...
    If not, the most important thing is that it functions!

    Thanks once more Angst. You've been an awesome help

  • #12
    New Coder
    Join Date
    Apr 2008
    Location
    Atlanta, GA
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok, last post with the final solution...
    This results nicely (without the "?") in:
    xx.asp --> www../xx.asp and
    xx.asp?idcategory=10 --> www../xx.asp?idcategory=10

    I also included that HTTP:// and HTTPS:// are handled correctly (when needed).
    Perhaps it's not the most beautiful code as-is, but it works!
    If anyone has suggestions on making the code "cleaner", please let me know.


    Code:
    <%
    Dim QS
    Dim QST
    QST = Request.ServerVariables("QUERY_STRING")
    IF QST <> "" then
    QS = "?" & QST
    Else
    QS = QST
    End if
    If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
    	Response.Status="301 Moved Permanently"
    	If Request.ServerVariables("HTTPS") = "off" then
    	Response.AddHeader "Location","http://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & QS
    	Else if Request.ServerVariables("HTTPS") = "on" then
    	Response.AddHeader "Location","https://www." & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL") & QS
    	End if
    	End if
    	Response.End
    End if
    %>
    Last edited by worldofrugs; 01-13-2010 at 03:53 PM. Reason: added HTTP + HTTPS to code


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •