Deprecated: Assigning the return value of new by reference is deprecated in /home/jlhdes/public_html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /home/jlhdes/public_html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/jlhdes/public_html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /home/jlhdes/public_html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/jlhdes/public_html/wp-settings.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/jlhdes/public_html/wp-settings.php:520) in /home/jlhdes/public_html/wp-content/plugins/bad-behavior/bad-behavior/screener.inc.php on line 8

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/jlhdes/public_html/wp-settings.php:520) in /home/jlhdes/public_html/wp-content/plugins/wp-referer.php on line 36

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/jlhdes/public_html/wp-settings.php:520) in /home/jlhdes/public_html/wp-content/plugins/wp-referer.php on line 36
301 Redirects in ASP on an IIS Server | JLH Design
  • JLH Design

  • 301 Redirects in ASP on an IIS Server

31st August 2006

301 Redirects in ASP on an IIS Server

posted in Webmastering |

Warning: copy() [function.copy]: Filename cannot be empty in /home/jlhdes/public_html/wp-content/plugins/mytube/mytube.php on line 220

Individual Page 301 redirects

To forward individual pages to a new location in IIS, please see this post.


www to non-www or non-www to www 301 redirects

To fix canonicalization issues of your domain in when using asp on an IIS server, specially in a shared hosting environment, sometimes you need to enter the code in on a page by page basis. Having your host set up the server to parse asp on all pages allows you to use this method on htm and html pages as well.

In developing the code for this I wanted it to have several functions:

  1. Be generic so I don’t need to enter the domain, so I can use the same file on all my domains.
  2. Pass on any server variables for dynamic pages.
  3. Redirect the default.asp page to “/”
  4. work in any subfolder as well for the default.asp fix.

At the top of each page on the site I place an included file calling up either version. The download(below) includes code to forward to the non-www and the www version of your site.

To forward to the WWW version via 301 redirect:

<%
'This code will redirect any asp page to the www version
'This code is provided as-is with no guarantee or warranty It is
'provided for free distribution as long as these comments are left in place.
'Designed by www.jlh-design.com 2006

Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM

'Get domain that the page is on

Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
'Check if URL is the www version

if left(Domain_Name, 3) <> "www" Then
HTTP_PATH = request.ServerVariables("PATH_INFO")

'Check if page is default.asp if so, redirect to "/".
'If other index page is used, such
'as index.asp the numbers in the right and len statement
'need to be changed, as well
'as the IF statment to indicate the index page.

If right(HTTP_PATH, 12) = "/default.asp" Then
TEMP_NUM = len(HTTP_PATH)-11
HTTP_PATH = left(HTTP_PATH,TEMP_NUM)
End If

' Sets the new URL settings with correct page

QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://www." & Domain_Name & HTTP_PATH
'This section passes on the query string variables
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if

' Send 301 response and new location

Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if

%>

To forward to the non-www version via a 301 redirect:

<%
'This code will redirect any asp page to the www version
'This code is provided as-is with no guarantee or warranty'It is provided for free distribution as long as these comments
'are left in place.
'Designed by www.jlh-design.com 2006

Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM

' Get domain name the page is on

Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))

' Check to see if www version

if left(Domain_Name, 3) = "www" Then

' Changes http path to non-www version

TEMP_NUM = len(Domain_Name)-4
Domain_Name = right(Domain_Name,TEMP_NUM)
HTTP_PATH = request.ServerVariables("PATH_INFO")

'Check if page is default.asp if so, redirect to "/".
'If other index page is used, such
'as index.asp the numbers in the right and len statement
'need to be changed, as well
'as the IF statment to indicate the index page.

If right(HTTP_PATH, 12) = "/default.asp" Then
TEMP_NUM = len(HTTP_PATH)-11
HTTP_PATH = left(HTTP_PATH,TEMP_NUM)
End If

' Sets the new URL settings with correct page

QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & Domain_Name & HTTP_PATH

'This section passes on the query string variables

if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if

' Send 301 response and new location

Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if

%>

Download

This entry was posted on Thursday, August 31st, 2006 at 5:49 pm and is filed under Webmastering. You can follow any responses to this entry through the RSS 2.0 feed. All comments are subject to my NoFollow policy. Both comments and pings are currently closed.

There are currently 13 responses to “301 Redirects in ASP on an IIS Server”

Why not let me know what you think by adding your own comment! All the cool kids are doing it.

  1. 1 MyAvatars 0.2 On September 11th, 2006, Anonymous said:

    Yes, I totally agree.
    Peace out!

  2. 2 MyAvatars 0.2 On January 4th, 2007, JLH said:

    No More Anonymous posts with the new format!

  3. 3 MyAvatars 0.2 On February 28th, 2007, Kevin Viner said:

    Any clue how to do this in ASP.NET with C#?

  4. 4 MyAvatars 0.2 On February 28th, 2007, JLH said:

    Not a clue here, but here’s a good resource, maybe John covers it.

    http://oy-oy.eu/huh/all/

  5. 5 MyAvatars 0.2 On February 28th, 2007, Kevin Viner said:

    Thanks for the link . . . couldn’t find it :(. Nobody really seems to know how to do it, and the host won’t do it through IIS.

  6. 6 MyAvatars 0.2 On May 9th, 2007, ron said:

    the www is working but it is NOT redirecting default.asp to “/”
    Any additional help? Thanks

  7. 7 MyAvatars 0.2 On May 9th, 2007, JLH said:

    Ron, That’s a fun quirk with IIS servers, you really can’t redirect to the “/” from default.asp if you are already on default.asp, as it will create an endless loop.

    It will do it however and get rid of the default.asp if you are directing from the non-conical domain.

  8. 8 MyAvatars 0.2 On May 9th, 2007, JohnMu said:

    While you can’t do the redirect on server-side, you can do it on the client side: http://oy-oy.eu/huh/redirect-default-aspx-asp-net/

    It’s a small piece of javascript, it’s not recognized by the search engines (so you’ll have to make sure that your site uses the root internally), but it will keep users from bookmarking or copy+paste-linking to the wrong URL.

  9. 9 MyAvatars 0.2 On February 24th, 2008, chitra said:

    sir,
    sorry to give troble,
    i have uploaded all the ecommerce file,then why are they not displayed on my site,what wrong i have done, i am new to this work, please solve my problem,

  10. 10 MyAvatars 0.2 On March 7th, 2008, Steve said:

    Great info here, I have scoured the internet for a solution to 301 the default.aspx to the root / - seems there is no answer to this problem within ASP.NET 2.0 or any version for that matter. The javascript solution is ok for going forward, but a 301 is what is really needed. Anybody have any other insights to this?

    Thanks Much
    Steve - Stoorz.com

  11. 11 MyAvatars 0.2 On October 28th, 2008, atul said:

    great info

  12. 12 MyAvatars 0.2 On November 2nd, 2008, Rajiv said:

    I put the include file for redirecting to WWW, however, I’m still trying to get my /default.asp page to be redirected to http://www.movingcompanies.tv. I was told that this is important so that the SE’s don’t think I have duplicate content; 1) for http://www.movingcompanies.tv, 2) for http://www.movingcompanies.tv/default.asp. Any guidance would be appreciated. Thanks.

  13. 13 MyAvatars 0.2 On November 6th, 2008, Mitesh Patel said:

    Great Information here i have some knowledge of .net but i thing its help full for new bench mark.

    Thanks.

  • Please Support

  • Marquette University

  • Sponsored

125x125

  • Donations


  • ;

Enter your email address:

Delivered by FeedBurner

rss posts
Spread the Word
Sphinn
delicious
digg
technorati
reddit
magnolia
stumbleupon
yahoo
google
  • Readers