The blog of Jafar Aliyev

Archive

Creation date:

redirect 301

We need page redirection from time to time. Especially when you move the content to new domain. Because some usefull pages are indexed by search engines and you don't want to lose clients bookmarking these pages.

mod_rewrite Redirect

With this rule we redirect the page old.php to the new.php. Write the following instruction in .htaccess file. Note, that R=301 means redirect permanently and instructs search engines to update their record regarding old.php. If you don't want to redirect permanently use 302 code unstead of 301. The 302 code tells clients to not cache the result because the resource is temporarily moved.

RewriteRule ^old\.php$ /new.php [R=301,L]

Look this table for the meanings of other codes. Although 302 is named "Found" the meaning of code is temporarily moved.

Status Code Description
300Multiple choices
301 Moved permanently
302 Found
303 See other
304 Not modified
305 Use Proxy
307Temporary Redirect

PHP Redirect

In this method we modify the header of page with PHP header() function

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

HTML meta tag Redirect

It will redirect the page to jsoft.ws in ten seconds

<meta http-equiv=refresh content=10; url=http://jsoft.ws/>

JavaScript Redirection

Because JavaScript is executed on client site it's least reliable method and depends on parameter of browser. Especially in IE you may get unexpected behaviour.

<script type="text/javascript">
window.open("http://jsoft.ws", "_top")
</script>

IIS (Internet Information services) Redirect

  • In internet services manager, right click on the file or folder you wish to redirect
  • Select the radio titled "a redirection to a URL"
  • Enter the redirection page
  • Check "The exact url entered above" and the "A permanent redirection for this resource"
  • Click on 'Apply'

ColdFusion Redirect

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

ASP Redirect

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

ASP .NET Redirect

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

JSP (Java Server Pages) Redirect

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

CGI PERL Redirect

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

Ruby on Rails Redirect

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

Author: Jafar N.Aliyev (Jsoft). Some methods are got from other sites and not checked by me.

Read also

HTML tips
Here I listed some useful HTML tips

Use site WITH WWW or WITHOUT WWW prefix

Download instruction in .htaccess

Hot linking prevention in .htaccess

© Copyright

All articles in this site are written by Jafar N.Aliyev. Reproducing of any article must be followed by the author name and link to the site of origin(this site). This site also keeps the same rules relative to the articles of other authors.