750th person to like us on FB will win our awesome cup and java t-shirt!

Jun-3-2010

301 redirects htaccess redirect 150x150 301 Redirects: When, Why and How To Use 301 RedirectsWhen you would need to use 301 redirects?

  1. You have a website with a lot of broken links and 404 errors – in this case you should redirect all problematic website pages to the corresponding live pages
  2. You have updated your website and your site structure has changed permanently – if your website is new that is not a big deal, but if you had a popular website with hundreds or thousands of pages ranking high in search engines then 301 redirect is the only solution
  3. url canonicalization – if you didn’t know www and non-www versions of your website are treated as separate urls by search engines, so pick one and stay consistent. If you pick http://www.example.com , then use 301 to redirect all the traffic from http://example.com. Also when dealing with url canonicalization, don’t forget to set your preferred domain in Google Webmaster Tools

How to use 301 Redirects

Setting 301 redirects in apache using .htaccess

In Apache Web server URL rewriting (including 301 redirects) can be done with the help of mod_rewrite engine. Redirects are done my modifying .htaccess file, there are also other cool things that can be done via .htaccess, but that’s for another post. Most common use of 301 redirects within the .htaccess file is redirecting all website traffic from www to non-www or the opposite. Edit the .htaccess file that can be found in your root folder and add at the end of the file the following:

To redirect all the traffic from the non-www to the www of your website with the “301 Moved Permanently” response. Edit the .htaccess file, that can be found in your root folder and add at the end of the file the following :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

To redirect all the traffic from the www to the non-www of your website with the “301 Moved Permanently” response:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Implementing .htaccess redirects for static pages:

redirect 301 /old-cat/old-page.htm http://www.example.com/new-page.htm

Setting Redirects in Windows IIS

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 redirection page
Check “The exact url entered above” and “A permanent redirection for this resource”
Click ‘Apply’

ColdFusion 301 Redirects

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader value="http://www.example.com">

PHP Redirect

<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com" );
?>

ASP 301 Redirect

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

ASP .NET 301 Redirect

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

JSP (Java) 301 Redirects

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

CGI PERL Redirect

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

Ruby on Rails 301 Redirect Example

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

Feel free to let me know if you have any tricks and tips on 301 redirects as I am most familiar with .htaccess redirects.

Share and Enjoy:
  • Twitter
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Diigo
  • LinkedIn
  • PDF
  • RSS
  • StumbleUpon
  • Tumblr

Leave a Reply