How to redirect using JavaScript, ColdFusion, PHP, ASP, ASP.net. JSP, PERL, Ruby, HTML or .htaccess?

Using a redirect allows a website owner to direct a website visitor from one webpage or website to another.  There are two popular ways to redirect a website visitor/search engine, "302 redirect" and "301 redirect".  The method of redirection is basically the same code-wise but on the server-side or search engine optimization view point they are completely different.

The examples below are small snippets of code using the 302 redirection method.  The method called a "302 redirect"; the 302 code is a HTTP status code which is interpreted as a "temporary move".  Below you will find how to code a 302 redirect using popular scripting languages.  Depending on your objective you may want to use the 301 redirection method.  The method is called a "301 redirect"; the 301 code is a HTTP status code which is interpreted as a "permanently moved".  The 301 redirect is a preferred method if you want to preserve your search engine ranking.

  • Javascript
  • HTML
  • ColdFusion
  • PHP
  • ASP
  • ASP.net
  • JSP (Java)
  • CGI/PERL
  • Ruby on Rails
  • .htaccess

302 Redirection using HTML (META tag)

The code below will execute once the page is fully loaded in the web browser - HTML code is executed on the client-side.  It is best to use the code below on a blank page so that additional items on the page doesn't delay the page load time.

HTML

  1. <META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.redirectadomain.com/">
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using JavaScript

The code below will execute once the page is fully loaded in the web browser - JavaScript code is executed on the client-side and can be disabled by the user.  It is best to use the code below on a blank page so that additional items on the page doesn't delay the page load time.

JavaScript

  1. <script type="text/javascript">
  2. <!--
  3. window.location = "http://www.redirectadomain.com/"
  4. //-->
  5. </script>
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using ColdFusion

The following ColdFusion code must be run on a server which can interpret ColdFusion coding - such servers include Microsoft IIS (Internet Information Server) with ColdFusion support.  Like all server-side languages, the server needs to interpret the ColdFusion code before rendering it.  When the server gets to line 1 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

ColdFusion

  1. <cflocation url="http://www.redirectadomain.com/">
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using PHP

The following PHP code must be run on a server which can interpret PHP coding - such servers include Apache Webserver as well as other Microsoft IIS (Internet Information Server).  Like all server-side languages, the server needs to interpret the PHP code before rendering it.  When the server gets to line 2 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

PHP (PHP: Hypertext Preprocessor)

  1. <?php
  2. header( 'Location: http://www.redirectadomain.com/' ) ;
  3. ?>
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using ASP

The following ASP code must be run on a server which can interpret ASP coding - such servers include Microsoft IIS (Internet Information Server) as well as other non-Microsoft servers using alternative implementations such as Sun ONE, Arrowhead ASP or Cloudfoundry ASP.  ASP (Active Server Pages) is a programming language created by Microsoft in the late 90's.  ASP is sometimes referring to as Classic ASP or ASP Classic and was Microsoft's first server-side programming language.  Like all server-side languages, the server needs to interpret the ASP code before rendering it.  When the server gets to line 1 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

ASP (Classic Active Server Pages)

  1. <% Response.Redirect "http://www.redirectadomain.com/" %>
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using ASP.net

The following ASP.net code must be run on a server which can interpret ASP.net coding - such as Microsoft IIS (Internet Information Server).  Like all server-side languages, the server needs to interpret the ASP.net code before rendering it.  When the server gets to line 1 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

ASP.net

  1. Response.Redirect "http://www.redirectadomain.com/"
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using JSP (Java)

The following JAVA code must be run on a server which can interpret JAVA coding - such as Tomcat.  Like all server-side languages, the server needs to interpret the JAVA code before rendering it.  When the server gets to line 1 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

JSP (JavaServer Pages)

  1. <% Response.sendRedirect("http://www.redirectadomain.com/"); %>
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using PERL/CGI

The following PERL code must be run on a server which can interpret PERL coding - such as Microsoft IIS (Internet Information Server) or Apache Webserver.  Like all server-side languages, the server needs to interpret the PERL code before rendering it.  When the server gets to line 2 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

Perl (Practical Extraction and Report Language) or CGI (Common Gateway Interface)

  1. $q = new CGI;
  2. print $q->redirect("http://www.redirectadomain.com/");
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using Ruby on Rails

The following PERL code must be run on a server which can interpret PERL coding - such as Microsoft IIS (Internet Information Server) or Apache Webserver.  Like all server-side languages, the server needs to interpret the PERL code before rendering it.  When the server gets to line 1 below it will redirect the page immediately.  If there are other elements on the page such as text, graphics, video, etc. they will all be ignored.

Ruby or Ruby on Rails

  1. redirect_to "http://www.redirectadomain.com/"
* replace http://www.redirectadomain.com/ with your own domain name.

302 Redirection using .htaccess

The following .htaccess directives must be run on a server which can understand .htaccess directives and have permission to override server configuration.  The .htaccess file is usually used on a Apache Webserver but can also work on Microsoft IIS (Internet Information Server) if configured to support .htaccess directives.  The directives act differently than server-side scripting because they are executed before any web pages or scripts.  When the server gets to line 1 below it will redirect the browser immediately.

.htaccess

  1. Redirect / http://www.redirectadomain.com/
* replace http://www.redirectadomain.com/ with your own domain name.
Bookmark and Share