View Full Version : Automatic redirect.
Hi all, I need a page to redirect from http://www.mysite.com/page.asp to http://mysite.com/page.asp on all pages on a site, is there an easy way to do that? My asp knowledge is very limited so I am stuck on this one..
Old Pedant
08-16-2010, 12:56 AM
If the URL http://www.mysite.com/page.asp does *NOT* cause an error, there's no easy way.
You would need EACH ASP PAGE to begin with
<%
url = Request.ServerVariables("URL")
If InStr( 1, url, "//www.", vbTextCompare ) > 0 Then
Response.Redirect Replace(url, "//www.", "//", 1, -1, vbTextCompare)
End If
%>
If the URL http://www.mysite.com/page.asp does *NOT* cause an error, there's no easy way.
You would need EACH ASP PAGE to begin with
<%
url = Request.ServerVariables("URL")
If InStr( 1, url, "//www.", vbTextCompare ) > 0 Then
Response.Redirect Replace(url, "//www.", "//", 1, -1, vbTextCompare)
End If
%>
Thanks, I think I can work that code out. Changed the domain on a site from www.mysite.com (http://www.mysite.com/) to mysite.com and it is causing a world of problems with the new certificate. There are a few static links out there that are causing the site to come up with an untrusted/invalid domain error…
Old Pedant
08-16-2010, 02:02 AM
Well, if the URL "www.mysite.com" doesn't exist--if you get a 404 error--then the problem might be a lot harder. If it exists but just is invalid for your certificates, et al., then the above should work.
The code above didn't quite work. but i used it with some other code i found by googling and came up with this.
<%
function curPageURL()
dim s, protocol, port
if Request.ServerVariables("HTTPS") = "on" then
s = "s"
else
s = ""
end if
protocol = strleft(LCase(Request.ServerVariables("SERVER_PROTOCOL")), "/") & s
if Request.ServerVariables("SERVER_PORT") = "80" then
port = ""
else
port = ":" & Request.ServerVariables("SERVER_PORT")
end if
curPageURL = protocol & "://" & Request.ServerVariables("SERVER_NAME") &_
port & Request.ServerVariables("SCRIPT_NAME")
end function
function strLeft(str1,str2)
strLeft = Left(str1,InStr(str1,str2)-1)
end function
url = curPageURL()
If InStr( 1, url, "//www.", vbTextCompare ) > 0 Then
Response.Redirect Replace(url, "//www.", "//", 1, -1, vbTextCompare)
End If
%>and i have just included that at the top of every page. it looses any GET vars but i can live with that, for now... :)
Old Pedant
08-16-2010, 06:50 PM
We can easily put back in the GET info.
Just change the line:
Response.Redirect Replace(url, "//www.", "//", 1, -1, vbTextCompare)
to
Response.Redirect Replace(url, "//www.", "//", 1, -1, vbTextCompare) & "?" & Request.QueryString
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.