Redirect webpages with HTML, PHP, .htaccess, Java+Script, CGI-Perl, ASP.NET and ColdFusion

Submitted by n8coder on Fri, 05/15/2009 - 00:04

Here are examples of code how to redirect an old page/url/domain to a new one with most popular client+server side programming/scripting languages. We use 301 http status code (permanent redirect). If you need temporary redirection use 302 status code instead of 301. It is better to prefer server-side redirection, because some clients are surfing with javascript and http-refresh disabled browsers. Do not forget to test your code first!

.htacces and Mod_Rewrite (Apache Webserver)

Put this code at the top of your .htaccess file, which should be located on root directory(ie: /home/USERNAME/public_html/.htaccess).

Redirect 301 /path/to/old-page.html http://www.example.com/new.html




HTML / XHTML

Put this code between <head> and </head> tags. Remember it will not work if client-side refresh disabled.

&lt;meta http-equiv="refresh" content="0; url=http://www.example.com/new.html">




PHP (PHP4 + PHP5)

Put this code at the top of .php file. Optional but redirect works only if no header-output already sent.

<?php
header
("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/new.html");
die();
?>




JavaScript

The right place is between <head> and </head> but it works mostly everywhere on page. Sometimes it is used to create a link without html-anchors (<a href=...>) with help of onClick event.


&lt;script language=&quot;JavaScript&quot;&gt;
&lt;!--
window.location = &quot;http://www.example.com/new.html&quot;;
--&gt;
&lt;/script&gt;




JAVA


<%
response.setStatus(301);
response.setHeader("Location", "http://www.example.com/new.html");
response.setHeader("Connection", "close");
%>




ASP


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




ASP.NET

&lt;script&gt;
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = &quot;301 Moved Permanently&quot;;
Response.AddHeader(&quot;Location&quot;,&quot;http://www.example.com/new.html&quot;);
}
&lt;/script&gt;




CGI/PERL


$q = new CGI;
print $q-> redirect("http://www.example.com/new.html");




ColdFusion

&lt;cfheader statuscode=&quot;301&quot; statustext=&quot;Moved permanently&quot;&gt;
&lt;cfheader name=&quot;Location&quot; value=&quot;http://www.example.com/new.html/&quot;&gt;




Ruby

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.example.com/"
end




Apache - modrewrite finetuning:



Add www: example.com to www.example.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]




Remove www: www.example.com to example.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]



Note: We try our best but there is no quarantee for any code on this site.

Submitted by Anonymous (not verified) on Fri, 05/15/2009 - 01:02

Permalink

This is how2 redirect with RUBY:

def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.example.com/"
end

Add new comment