Navigator


Archive

201139
201230
201312
20151
201633
201755
201865
201955
20234

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
300 Multiple choices
301 Moved permanently
302 Found
303 See other
304 Not modified
305 Use Proxy
307 Temporary 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

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

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