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.

<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.

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.

<script language="JavaScript">
<!--
window.location = "http://www.example.com/new.html";
-->
</script>




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


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




CGI/PERL


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




ColdFusion


<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.example.com/new.html/">




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