Skip to main content
CafeWebmaster

Main navigation

  • Tools
    • MD5 converter
    • SHA1 converter
    • Base64 encode
    • Base64 decode
    • HTML entities encode
    • HTML entities decode
    • Raw url encode
    • Raw url decode
    • UTF8 encode
    • UTF8 decode
    • Unix Time to Human Date converter
    • Remove duplicate lines
    • Sort text lines
    • Backwards - Mirrored Text
    • PiRaTe tExT
    • Your IP Address & Hostname
  • Forums
  • Free PHP Scripts
    • Image watermark with PHP
    • Page generation time with PHP
  • CSS
    • CSS hidden left menu
    • CSS horizontal menu
  • Free Fonts
  • Image Editors
  • Tutorials
    • Check password strength / safety with PHP and Regex
    • Cómo hacer que su sitio web popular!
    • How2 make a website popular
    • How2 secure your website
    • Kennwort Sicherheit mit PHP und Regex Prüfen
    • Page generation time and http-referrers with php
    • Redirect webpages with HTML, PHP, .htaccess, Java+Script, CGI-Perl, ASP.NET and ColdFusion
  • New
User account menu
  • Log in

Breadcrumb

  1. Home

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

By n8coder, 15 May, 2009

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.

  • Log in to post comments

Anonymous (not verified)

14 years 4 months ago

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