Welcome to WebmasterWorld Guest from 34.238.189.171

Forum Moderators: ocean10000

Message Too Old, No Replies

ASP 301 Redirect Question

     
2:48 pm on Mar 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


My website has 100,000 or so ASP pages that I would like to do a 301 redirect. I asked my host if the could do the IIS 301 redirect tweak and of course they said no. They said I would need to do the redirect at the page level using the ASP redirect code:

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

This is fine, I guess, but I am confused as to how I change the redirect URL on each page.

For example:

www.example/viewitem.asp?=a12349 www.example/viewitem.asp?=a12350
www.example/mainpage.asp www.example.com/viewcategor.asp?=cat17

The variations go on and on 100,000 times with new pages added daily.

If I use the ASP redirect code, do I need to alter each piece of code to reflect the exact page to redirect to? (100,000 times and on the fly) For example, below:

********************************************

`<--- %@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" Response.AddHeader "Location", " www.example/viewitem.asp?=a12349"
>`

*********************************************
`<--- %@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" Response.AddHeader "Location", " www.example/viewitem.asp?=a12350"
>`
*********************************************
`<--- %@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" Response.AddHeader "Location", " www.example/mainpage.asp"
>`
********************************************
`<--- %@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" Response.AddHeader "Location", " www.example.com/viewcategor.asp?=cat17"
>`
********************************************

Or is there a ‘catch all’ that will just direct browsers and spiders to the www version of each page?

As for my HTML pages I am apparently out of luck. However, I could solve half the problem if I could figure out an easy way to implement the ASP redirect on a dynamic, database driven ASP site.

[edited by: Xoc at 1:44 am (utc) on April 26, 2006]
[edit reason] examplified urls [/edit]

4:40 pm on Mar 7, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2006
posts:44
votes: 0


You can obtain the host name in the HTTP header using serverVariables.
<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
%>

Now check the Domain_Name to see if it is the www version, if not, then 301 redirect to the properly constructed URL using the "http://www.example.com", PATH_INFO and QUERY_STRING serverVariables.

[edited by: Xoc at 1:44 am (utc) on April 26, 2006]
[edit reason] examplified urls [/edit]

7:03 pm on Mar 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Leapforward, thanks for the info. I know very little about ASP coding (nothing) so I was wondering if you could give me a complete example?

Say I wanted to redirect on the fly using your method, anyone or any spider coming to:

http://example.com/items/stock.php?=17354

to

http://www.example./items/stock.php?=17354

What ASP snippet(s) would I put on the top of all my asp pages?

[edited by: Xoc at 1:45 am (utc) on April 26, 2006]
[edit reason] examplified urls [/edit]

8:18 pm on Mar 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


I tried the following ASP snippet and am getting an error:

<%

If Request.ServerVariables("SERVER_NAME")="example.com" then
strRedirectURL = "www.example.com" & Request.ServerVariables("URL")
if Request.QueryString <> "" then strRedirectURL = strRedirectURL & "?" & Request.QueryString
Response.Status="301 Moved Permanently" Response.AddHeader "Location", strRedirectURL
end if

%>

I get the following error when trying to view any page I put this on:

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/index.asp, line 18

Response.Status="301 Moved Permanently" Response.AddHeader "Location", strRedirectURL
----------------------------------------^

[edited by: Xoc at 1:46 am (utc) on April 26, 2006]

9:16 pm on Mar 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2003
posts:1184
votes: 0


These need to be 2 seperate lines
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", strRedirectURL
10:35 pm on Mar 7, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Getting close on this...

<%

If Request.ServerVariables("SERVER_NAME")="mydomain.com" then
strRedirectURL = "www.example.com" & Request.ServerVariables("URL")
if Request.QueryString <> "" then strRedirectURL = strRedirectURL & "?" & Request.QueryString
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", strRedirectURL
end if

%>

When http://example.com/ is entered it returns:

http://example.com/www.example.com/index.asp

[edited by: Xoc at 1:47 am (utc) on April 26, 2006]

2:36 am on Mar 8, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2006
posts:44
votes: 0


Here is a simple example that will display the URL that you want to 301 redirect to.

<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.mydomainname.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & domain_name & "/" & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
response.Write theURL
end if
%>

You can change the:
response.write theURL

to:
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL

3:23 pm on Mar 8, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Leap,

I tried your code and with http://example.com and I get the following redirect error.
******************************
The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

* This problem can sometimes be caused by disabling or refusing to accept cookies.
******************************

Here is the code snip I used:

<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.example.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & domain_name & "/" & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
end if
%>

Also, the URL that is in the address bar is:http://example.com//index.asp

Boy do I wish I went with PHP/Apache way back when so I could just enter a couple lines of code to my .htaccess file and the 301 redirect would be done.

[edited by: Xoc at 1:47 am (utc) on April 26, 2006]

8:46 pm on Mar 8, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2006
posts:44
votes: 0


try changing this line:

theURL = "http://" & domain_name & "/" & HTTP_PATH

to

theURL = "http://" & domain_name & HTTP_PATH

see if that works

9:40 pm on Mar 8, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Feb 21, 2002
posts:228
votes: 0


I'm glad to have found this thread. I was about to post almost the same question.

I do not know anything about the structure and use of .NET or .asp pages.

I handle marketing for a site and the webmaster constructed the site in .asp.

From the information I have gathered so far, due to the Big Daddy update, we have been trying to determine what shot our 3000+ page site into supplementals.

One: We have duplicate Titles generated on each e-commerce page. If page titles are changed to be very specific for each product on each page, does each page have to be hand-altered? Or is there a way to assign Titles that are not duplicate at all?

Two: The webmaster could care less about SEO. I have to try to figure that out and tell him. I asked him how the multiple domains owned by the company were handled to redirect to the main www.example.com web site files. His reply was - "Server-side ASP. If the URL is not www.example.com, we redirect to www.example.com. This will still produce an HTTP GET status code of 200..."

Elsewhere on webmasterworld, I read the following:

"If you have your www and non-www pages both accessible on your site (with 200 status) then you have "duplicate content". In this case you must use the 301 redirect. It will get the whole site properly listed and fully indexed as ONE version.
If you do not use the redirect you will end up with split PR and many pages that show as URL-only, and many pages that show as supplemental for both the www and the non-www version, and maybe quite a few pages that fail to get indexed at all under either version."

If the statement directly above is the case, what specifically should I tell the webmaster to do? How should multiple domains be structured to 301 to the main web site that is proper on an asp site?

We do run our own server, so server-side includes and any type of server-side techniques could be implemented.

[edited by: Xoc at 1:48 am (utc) on April 26, 2006]

6:57 pm on Mar 9, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


try changing this line:

theURL = "http://" & domain_name & "/" & HTTP_PATH

to

theURL = "http://" & domain_name & HTTP_PATH

see if that works

I changed to:

<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.example.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & domain_name & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
end if
%>

Now the domain that appears in the address bar is correct but I am still getting the error: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Surely there must be someone doing an ASP redirect on a dynamic website that can flip me the snippet of ASP code they are using to do a page based 301 redirect.

[edited by: Xoc at 1:49 am (utc) on April 26, 2006]

6:44 am on Mar 10, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2006
posts:44
votes: 0


try this:

add (line before response.staus):

    Response.Clear

add (line after the one with response.add header

    Response.Flush
    Response.End

8:05 pm on Mar 10, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Leapforward,

Thanks for your persistence. However, I made the changes and I am still getting the same error when typing in example.com: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

My snippet is now:

<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.EXAMPLE.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & domain_name & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if
%>

[edited by: Xoc at 1:49 am (utc) on April 26, 2006]

4:57 pm on Mar 16, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


I thought I would give it one last try here at WebmasterWorld...

I am still on the hunt for an ASP 301 redirect snippet that works for a large dynamic website. I can find, snippets to redirect a single page, but I have 100,000s of pages with different URLs so I need something that will work site wide. Someone out there must have a proven snippet of 301 ASP code on their windows hosted dynamic site that they use?

I am on a shared windows box so my only hope to do a 301 redirect is via code on every page on my site. An IIS level solution is not allowed.

Any ideas, anyone?

5:10 pm on Mar 16, 2006 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member pageoneresults is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 27, 2001
posts:12172
votes: 61


An IIS level solution is not allowed.

Move to a host that has ISAPI Rewrite installed and offers support for implementation. The only way to effectively manage what you have going on is through an ISAPI filter. A few lines in your httpd.ini file and you're ready to go.

I believe there are some built in facilities for basic rewriting in ASP.NET. But, my understanding is that ISAPI Rewrite takes it way beyond the basics.

10:51 pm on Mar 16, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Can anyone sticky me a Windows host that allows this on a shared hosting plan? The place I use is quick with support (I guess it doesn't take long to say no) but they don't let their shared clients do anything outside the box.
10:59 pm on Mar 16, 2006 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member pageoneresults is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 27, 2001
posts:12172
votes: 61


The place I use is quick with support (I guess it doesn't take long to say no) but they don't let their shared clients do anything outside the box.

I'd try to sell your host on the fact that having ISAPI Rewrite installed and available for clients would put them a step or two above their competition. At $69.00 for a single server license (unlimited domains), it is the only solution available for Windows that is comparable to the Apache Mod_Rewrite.

You are going to find it very difficult to find any public hosting companies that offer the ISAPI filters. Why? Because most, if not all, are afraid of installing something they are not familiar with.

Hey, all you Windows hosting companies! Get your act together and start offering rewrite capabilities for your clients. If you don't, we're going to lose more market share to the UNIX side. What the heck is the holdup? The installation of ISAPI Rewrite takes about 15 minutes. After that, you drop an .ini file in the root of each web and let the client configure from there. What I recommend is that you have a default .ini and provide commented examples that they can use for simple redirect routines that they'll need on a regular basis.

11:08 pm on Mar 16, 2006 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member pageoneresults is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 27, 2001
posts:12172
votes: 61


One: We have duplicate Titles generated on each e-commerce page. If page titles are changed to be very specific for each product on each page, does each page have to be hand-altered?

I sure hope not. Are you generating static html pages from your database? Or, are you generating pages on the fly based on the request?

Or is there a way to assign Titles that are not duplicate at all?

Absolutely. Use a variable from the database. I'll assume you have titles for each product?

<title>Insert Variable Here</title>

Two: The webmaster could care less about SEO. I have to try to figure that out and tell him. I asked him how the multiple domains owned by the company were handled to redirect to the main www.company.com web site files. His reply was - "Server-side ASP. If the URL is not www.company.com, we redirect to www.company.com. This will still produce an HTTP GET status code of 200..."

Ummm, you may want to seek some technical help if you have your own servers.

Elsewhere on webmasterworld, I read the following: "If you have your www and non-www pages both accessible on your site (with 200 status) then you have "duplicate content". In this case you must use the 301 redirect. It will get the whole site properly listed and fully indexed as ONE version.

If you do not use the redirect you will end up with split PR and many pages that show as URL-only, and many pages that show as supplemental for both the www and the non-www version, and maybe quite a few pages that fail to get indexed at all under either version."

If the statement directly above is the case, what specifically should I tell the webmaster to do? How should multiple domains be structured to 301 to the main web site that is proper on an asp site?

Multiple domains? 301 to main website? How many domains are there?

11:37 pm on Mar 16, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Feb 21, 2002
posts:228
votes: 0


The comment about using variables from the database was spot-on PageOne. We accomplished that about 5 days ago. The transition was smooth and pulled the unique titles from the product names.

About the multiple-domains: We had one main site. The site owner bought 7 other domains that covered category types consistent with the product line.

I found some information out on the net how to handle these as well. This is "supposedly" the correct way to handle multiple domains.

We set up an account on another server away from our main site/server.

One secondary domain (from the multiple group) was chosen as the Main Secondary domain.

We 301'd the remaining 6 domains (all have their own basic account) to the Main Secondary Domain. We then 301'd the Main Secondary Domain to the Primary Domain Web Site.

Now - supposedly, that is the proper way to handle the multiple - domain issue. I'd like to hear if I've been misinformed ;-)

Thanks!

4:55 pm on Mar 17, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


You are going to find it very difficult to find any public hosting companies that offer the ISAPI filters. Why? Because most, if not all, are afraid of installing something they are not familiar with.

It took one call to the Windows hosting service I was going to go with 3 years ago instead of the one I currently use. But they do offer ISAPI Rewrite on their shared plans. Too bad I wanted to save that $15 a month way back then...

Now I need to get my nerve up to move my site, SQL and all.

6:27 pm on Mar 23, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Finally! They said it was not possible, but after searching and asking questions on many forums, someone figured out how to do an ASP redirect for a dynamic site. The previous code I was given here was close but put my web page in an infinate loop.

So if you have a dynamic ASP site and your Windows IIS host won't allow you to do the server-side IIS tweak, here is the code you can add to all the ASP pages on your website:

<%
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.example.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://www.example.com" & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if
%>

Of course, be sure to replace domain.com with your own domain name and in lower case only.
I tested this code on my site and it works great.

If you use this code, I suggest you save it in a file named redirect.asp and then add to the top of each file on your site the following snippet:

<!-- #Include file="redirect.asp" -->

If you have files in sub-directories add the appropriate path info or use a fully qualified path.

Enjoy!

[edited by: Xoc at 1:51 am (utc) on April 26, 2006]

8:31 am on Mar 26, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 12, 2004
posts:7
votes: 0


Great topic. Is it possible to put a response.redirect before the response.end? I have just done this to prove that the script was working. Will it mess things up if i leave it in place?

Cheers

8:49 am on Mar 26, 2006 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member pageoneresults is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 27, 2001
posts:12172
votes: 61


So if you have a dynamic ASP site and your Windows IIS host won't allow you to do the server-side IIS tweak, here is the code you can add to all the ASP pages on your website:

Wouldn't this short block of code suffice?

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

Or, am I missing something? :(

And this short block of code for ASP.NET...

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.example.com/new-page.asp");
}
</script>
9:19 am on Mar 26, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 12, 2004
posts:7
votes: 0


I added the response.redirect so that the URL changed in the address bar. Is this wrong?
1:16 pm on Mar 26, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 12, 2002
posts:1482
votes: 0


The Response.Redirect() method actually gives the browser a 302 response header, and will overwrite your 301 command.
4:05 pm on Mar 26, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 12, 2004
posts:7
votes: 0


OK this is the script that I have implemented if anybody would like it.

Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
if domain_name <> "www.example.com" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")
If Left(HTTP_PATH, 8) = "/default" Then
HTTP_PATH = ""
End If
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://www.example.com" & HTTP_PATH
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if

I added in..

If Left(HTTP_PATH, 8) = "/default" Then
HTTP_PATH = ""
End If

So the URL that it is sent to does not have the /default.asp on the end of it.

If anybody can see anything wrong with this script. I would love to here.

I have also changed my internal back links to <a href=&#8221;/&#8221;> as apposed to to <a href=&#8221;default.asp/&#8221;> .I notice that Google uses this technique for its home logo link.

[edited by: tedster at 4:47 pm (utc) on Mar. 26, 2006]
[edit reason] use example.com in code [/edit]

11:45 pm on Mar 26, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 18, 2003
posts:745
votes: 0


Wouldn't this short block of code suffice?

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

Yes your code would work for one page, but I was looking for a one size fits all snippet of code that would redirect to the proper URL on the fly for each page on a large dynamic website.


Ipjunkie,

I added your tweak for the default.asp or in my case the index.asp at the end of it and fixes the one problem I had with the code on my home page. As far as I can tell, your snippet works great.

7:31 am on Mar 27, 2006 (gmt 0)

New User

10+ Year Member

joined:Oct 12, 2004
posts:7
votes: 0


I see what you mean. I put the code at the top of my functions include, so it works on all pages. I think this is the simplest way for all of my asp sites on a shared platform.

With asp.net i can just add a rewrite rule into the web.config file (which i haven’t done yet)