How to redirect web pages, what is 301 redirect and how to do redirects in different technologies
Sometimes we move domains, rename pages, migrating to different hosting account and different content management systems. This all result in URL change (changing of address) of our old pages. With proper usage of redirects this move will be easy and as search engine friendly as we could make it.
What is a 301 redirect?
301 Redirect – tells Search Engines that your old page has “permanently moved” to the new location – new page. 301 Redirect could also affect group of pages or the whole site altogether. Your old site will be gradually removed from the Search Engine’ indexes and your new site will take its place. The advantage of using this approach is that the 301 Redirect will also transfer the link popularity gained by your old site, to the new one. The bad news is that some search engines might not recognize that you’ve simply changed domains and will subject your new domain to the aging delay as if it were a brand new site. The result is that your rankings might suffer, especially in more competitive markets. Regardless of that 301 redirect is the best recommended technique to do redirects. 301 redirect will work just fine in most cases, such as:
- domain.com -> www.domain.com
- domain.com/old-page -> domain.com/new-page
- domain.com/old-category/page1 -> domain.com/new-category/page1
If Google and other search engines can access www.site.com/page1 and site.com/page1 as the same page – they would think that your site has 2 different pages with duplicate content. Duplicate content will cause your rankings (measure of “respect” from search engines) to be penalized. This is easy to avoid with the help of 301 redirect. In this case you need to forward all ‘non-www’ requests for your site to ‘www…’ requests. Here is the code to insert into .htaccess file in the root directory of your web hosting space:
########## Begin - prohibit usage of URLs without "www" ########### RewriteEngine On RewriteCond %{HTTP_HOST} . RewriteCond %{HTTP_HOST} !^www.yourwebsitename.com [NC] RewriteRule (.*) http://www.yourwebsitename.com/$1 [R=301,L] ########### End - prohibit usage of URLs without "www" ##########
How to do redirects in PHP
<?php header("Location: http://www.newsite.com/"); ?> or: <?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.newsite.com"); ?>
How to do redirect in JSP (Java)
<% response.setStatus(301); response.setHeader( "Location", "http://www.newsite.com/" ); response.setHeader( "Connection", "close" ); %>
How to do redirect in CGI PERL
$q = new CGI; print $q->redirect("http://www.newsite.com/");
How to do redirect in Ruby on Rails
def old_action headers["Status"] = "301 Moved Permanently" redirect_to "http://www.newsite.com/" end
How to do redirects in ASP
<%@ Language=VBScript %> <% Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://www.newsite.com/" %>
How to do redirects in ASP .NET
<script runat="server"> private void Page_Load(object sender, System.EventArgs e) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location","http://www.newsite.com"); } </script>
More readings: Apache .htaccess tutorial
Credits: WebConfs