PDA

View Full Version : 301 Redirect in ASP


Chatmaster
03-03-2007, 08:09 AM
I have been searching for a way to do a 301 redirect in ASP for some time. I have always been able to simply use IIS to accomplish this but always had a problem with shared hosting, as I had no access to IIS. I found this article about 301 redirect with ASP (http://www.webconfs.com/how-to-redirect-a-webpage.php). This is most helpfull to redirect individual pages and I thought I'd share it with you.

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


301Redirect using asp.net
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>

Paz
03-03-2007, 08:48 AM
Looks good. While we're on favourite ASP scripts, here's an ASP script to redirect from your non-www pages to the www versions. This is important to avoid a duplicate content problem in Google for having two sites http://example.com and http://www.example.com with the same content.

This redirect is particularly important if you have lots of similar pages, eg an online catalogue.

Actually the best way to do this is to get your hosting company to set it up for you on the server, but not all will help.

This only works on ASP files as the default in the root eg default.asp.

If you're you're using index.htm, for example, then you have a very difficult decision! If all your internal navigation points to /index.htm then you will lose homepage pagerank, and you could even be "penalised" for a few months after you've updated your internal navigation to point to "/" (never link home with /default.asp /index.html for this reason).

If your site is new and not ranking, then I would recommend that you you implement this ASP non www redirect sooner rather than later.

The ASP code in the default homepage is pasted below. Under that you paste your old index.htm code, update your internal navigation and set the root to point to default.asp using your server control panel.

Edit www.example.com 2 times to point to your domain.

<%
dim hostname
dim pathinfo
dim bRedirect
dim MainDomain
hostname = request.servervariables("HTTP_HOST")
pathinfo = request.servervariables("PATH_INFO")
MainDomain = "www.example.com"
MainDomain = "www.example.com"
if pathinfo <> "" then
if instr(lcase(pathinfo),"default.asp") > 0 or instr(lcase(pathinfo),"index.asp") > 0 or instr(lcase(pathinfo),"index.aspx") > 0 then
MainDomain = MainDomain &"/" & mid(pathinfo,2,instrrev(pathinfo,"/")-1)
else
MainDomain = MainDomain & pathinfo
end if
else
MainDomain = MainDomain & "/"
end if
if left(hostname,instr(hostname,".")) <> "www." then
bRedirect = true
end if
if instr(lcase(hostname),"default.asp") > 0 then
bRedirect = true
else
end if
if bRedirect then
response.status = "301 Moved Permanently"
response.addheader "Location", "http://" & MainDomain
response.end
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
etc...