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.
Similar entries
- Change background color onmouseover with Javascript
- Check password safety with JavaScript while typing
- Url manipulation with mod_rewrite and php-catcher for beginners
- CSS Text effects for MSIE
- Prevent hotlinking with htaccess and mod_rewrite
- Redirect a query to multiple search engines with one form and javascript
- .htaccess examples
- Register Globals Emulator for PHP
- DropDown / Rollover Menu with pure CSS / HTML
- Pure CSS Mouseover Menu without Javascript
- Web Application Security Papers
- Basic HTML Table with rounded corners
- File Creation problem under Windows 7 with Notepad++ FTP
- Mouseover images with CSS
- Web 2.0 Style two Side Background, Dark to Light Effect
- Include both php/ html files, catch & assign output to a string variable
- Remove underline from text-links (html-tag a)
- A HTML-Example Page
- A simple website structure

Comments
This is how2 redirect with
This is how2 redirect with RUBY:
Very nice post.Provided all
Very nice post.Provided all the redirection method for all the programming languages.Very informative post.
Post new comment