...

View Full Version : Resolved 301 redirect from non-www to www - RESOLVED



worldofrugs
01-12-2010, 04:51 PM
I have a file 301.asp with the content (left the '< %' and '% >' out):


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:


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?

angst
01-12-2010, 05:10 PM
seems like it should work, but I don't have an asp server to test on.
try debugging.

see if this gets hit:


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.

worldofrugs
01-12-2010, 05:48 PM
Changed the 301.asp to:



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.

angst
01-12-2010, 06:06 PM
hmm ok, try a response write on this:



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

worldofrugs
01-12-2010, 06:20 PM
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 :thumbsup:

angst
01-12-2010, 06:26 PM
hmm, i don't see the URI variable in the list: http://www.w3schools.com/asp/coll_servervariables.asp

maybe try this:


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

worldofrugs
01-12-2010, 07:09 PM
You the man!!!!


That works great!

Made my final code now:

<%
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 :thumbsup: and providing a solution that prevented me from going like this: :eek:

angst
01-12-2010, 07:37 PM
lol,, no problem.

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



<%
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
%>

worldofrugs
01-12-2010, 07:45 PM
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?

<%
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
%>

angst
01-12-2010, 07:48 PM
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.

worldofrugs
01-12-2010, 07:51 PM
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 :thumbsup:

worldofrugs
01-12-2010, 08:44 PM
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.



<%
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
%>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum