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

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.

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

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

Comments

This is how2 redirect with

This is how2 redirect with RUBY:

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

Very nice post.Provided all

Very nice post.Provided all the redirection method for all the programming languages.Very informative post.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <pre> <h1> <h2> <h3> <h4> <h5> <h6> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <div> <style><img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

CafeWebmaster.com(CW) is a free online community for webdevelopers and beginners. Anybody can share their code, articles, tips, tutorials, code-examples or other webdesign related material on the site. Newbies can submit their questions and reply to existing questions. CW does not guarantee or warrant reliability of code, data and information published on the site. Use the site on your own risk. The site takes no responsibility of direct or indirect loss or any kind of harm to its users. The site also doesn't take responsibility of infected files or source code with any kind of infection or viruses, worms, spywares, malwares, trojan horses. CW reserves the right to edit, move, or delete any of content for any reason.