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)
HTML
- <META HTTP-EQUIV="Refresh" CONTENT="0; URL=http://www.redirectadomain.com/">
302 Redirection using JavaScript
JavaScript
- <script type="text/javascript">
- <!--
- window.location = "http://www.redirectadomain.com/"
- //-->
- </script>
302 Redirection using ColdFusion
ColdFusion
- <cflocation url="http://www.redirectadomain.com/">
302 Redirection using PHP
PHP (PHP: Hypertext Preprocessor)
- <?php
- ?>
302 Redirection using ASP
ASP (Classic Active Server Pages)
- <% Response.Redirect "http://www.redirectadomain.com/" %>
302 Redirection using ASP.net
ASP.net
- Response.Redirect "http://www.redirectadomain.com/"
302 Redirection using JSP (Java)
JSP (JavaServer Pages)
- <% Response.sendRedirect("http://www.redirectadomain.com/"); %>
302 Redirection using PERL/CGI
Perl (Practical Extraction and Report Language) or CGI (Common Gateway Interface)
- $q = new CGI;
302 Redirection using Ruby on Rails
Ruby or Ruby on Rails
- redirect_to "http://www.redirectadomain.com/"
302 Redirection using .htaccess
.htaccess
- Redirect / http://www.redirectadomain.com/