Pin Me

The Basics of 301 Permanent Redirects with ASP, ASP.NET, PHP, and JSP

written by: John Garger•edited by: Rebecca Scudder•updated: 8/26/2010

In this third article in a series on Meta Refreshes and 301 Permanent Redirects, learn how to accomplish web page redirects with header inserts.

  • slide 1 of 6

    In the first article in this series, we discussed why Meta Refreshes are easy but not the optimal method of redirecting visitors to other web pages, directories, and domains on the Web. We concluded that Meta Refreshes may make your web page or domain rank lower on a Search Engine Results Page (SERP) because it may appear to search engines such as Google, Yahoo, and Bing that you are attempting to rank high in the search engine index for one content while sending visitors to some other content.

    In the second article in this series, we discussed that 301 Permanent Redirection is the preferred method of redirecting visitors. The search engines will not typically penalize you on a SERP for redirecting web content in this way. However, to accomplish the 301 Redirects discussed in that article, you need administrative access to your web server.

    If you have your web site hosted in a shared hosting plan this can be a problem because web hosting companies rarely give you root access to the web server. Short of having the web host do the redirection for you, you may have no alternative but to do the 301 Permanent Redirection right on your web pages themselves.

    Using ASP, ASP.NET, PHP, and JSP you can add a Header Insert to the top of every page you want to redirect elsewhere to avoid having to use a Meta Refresh. Read to learn how to add a Header Insert in ASP, ASP.NET, PHP, and JSP web pages to do a 301 Permanent Redirect.

  • slide 2 of 6

    301 Permanent Redirects Using Active Server Pages (ASP)

    Although ASP.NET is on the verge of completely replacing Active Server Pages, ASP is still the choice of many websites because it is simple to use and offers both novice and advanced users a lot of options, especially in making queries to small databases.

    To do a 301 Permanent Redirect on an ASP web page, add the following code to the top of each web page you want to redirect permanently:

    <%

    Response.Status = “301 Moved Permanently"

    Response.AddHeader “Location", “http://www.newlocation.com/newwebpage.asp"

    %>

    After you upload the page, check to see if the 301 redirection is working. If not, check the code in the header for mistakes.

  • slide 3 of 6

    301 Permanent Redirects Using ASP.NET

    ASP.NET is Microsoft’s new incarnation of Active Server Pages (ASP) and will eventually replace ASP as a web scripting language. To do a 301 Permanent Redirect on an ASP.NET web page, add the following code to the top of each web page you want to redirect permanently:

    <script runat="server">

    private void Page_Load(object sender, System.EventArgs e)

    {

    Response.Status = “301 Moved Permanently";

    Response.AddHeader(“Location", “http://www.newlocation.com/newwebpage.aspx");

    }

    </script>

    Again, check to see that the 301 Redirect is working after you upload your pages to your web server.

  • slide 4 of 6

    301 Permanent Redirects Using PHP

    A scripting language developed in the early days of the Internet and e-commerce, PHP has evolved into a powerful language that competes directly with ASP.NET in many regards. To do a 301 Permanent Redirect on a PHP web page, add the following code to the top of each web page you want to redirect permanently:

    <?php

    header(“HTTP/1.1 301 Moved Permanently");

    header(“Location: http://www.newlocation.com/newwebpage.php");

    exit();

    ?>

    Once again, after you upload your web pages, be sure that the 301 Permanent Redirect is working.

  • slide 5 of 6

    301 Permanent Redirects Using JSP

    As a Java-based Web development tool, JavaServer Pages (JSP) is the scripting language of choice for many web applications. To do a 301 Permanent Redirect on a JSP web page, add the following code to the top of each web page you want to redirect permanently:

    <%

    response.setStatus(301);

    response.setHeader(“Location", “http://www.newlocation.com/newwebpage.jsp");

    response.setHeader(“Connection", “close");

    %>

    Always be sure to check that the 301 Permanent Redirect is working after you upload your web pages to your web server.

  • slide 6 of 6

    Conclusion

    When you don’t have administrative access to your web server or you just want to permanently redirect one or a few pages, using the 301 Permanent Redirect code in this article is appropriate and simple. Just remember that in order to do a redirect this way, you need to edit and add code to every page you want to redirect. It is an inefficient method but it gets the job done if you need to redirect web content in a hurry.

Meta Refreshes and 301 Permanent Redirects with Internet Explorer 8, Apache Web Server, and Microsoft Internet Information Service (IIS) 7.0

Learn all about Meta Refreshes and 301 Permanent Redirection of Web content using IE8, Apache, and IIS 7. Also, get tips on how to implement a 301 Permanent Redirect in Apache and IIS.
  1. Learn about Automatic Redirection to New URLs in Internet Explorer 8
  2. How to Implement 301 Redirects in Apache and IIS 7
  3. The Basics of 301 Permanent Redirects with ASP, ASP.NET, PHP, and JSP