What is a Redirect?
301 redirect is the most efficient and Search Engine Friendly method for webpage redirection.
301 Redirect
Anytime you move a page, or change the URL that is used to access a specific page or set of pages, you need to setup a 301 Redirect.
The 301 status tells the browser that the page has been moved permanently. This is extremely important to search engines, 301 redirects are a critical step in ensuring your website is search engine friendly.
One of the trickiest things to avoid when continuing to build your site is the duplicate content pages. Having two pages that are essentially identical does not hurt your website from a visitor standpoint, it can be see by Google as spam. Google recommends using a 301 redirect.
Setting Up a 301 Redirect
Properly setting up a redirect can be very easy to do if you have a small site or simple redirection. If you have a large website or are changing the way you link to your pages, setting up a redirection can be complicated.
The examples listed here assume that you are running on an Apache webserver. If your website is running on a Windows platform, you should contact your webhost to figure out how to setup a 301 redirect.
.htaccess file
The htaccess file is a configuration file where redirections take place, which is used on Apache based web servers to control many features of the server. It is always listed as .htaccess (with the period in front).You can find a great tutorial on the .htaccess file over at Javascriptkit.
Moving a Page
If you are moving a page or a directory from one place to another, setting up a redirect is relatively easy to do. Open up your .htaccess file (or create a new file that you will call .htaccess) and paste in the following line:
Redirect 301 page.html http://www.domain.com/newlocation.html
In the above line all you need to do is replace the ‘page.html’ with the page or directory that is being moved and replace the ‘http.://www.domain.com/newlocation.html’ with the location of the new file. That’s all there is to it! A search engine spider will now know that if it tries to access page.html, it should update its listing to the new location given.
This could also work for changing your domains as well. If you are changing domains, you would write:
Redirect 301 / http.://newdomain.com/
Please REPLACE www.newdomain.com with your actual domain name.
Redirect Old domain to New domain
Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Please REPLACE www.newdomain.com with your actual domain name.
You Are Changing from HTML to PHP or ASP
As technology improves, so should our websites. This often requires that we change the technology that runs our websites. Many websites started out as plain HTML but later find the need to move to PHP or ASP to give their users the best experience possible.
There are a few options availabe in this situation. The solution we will offer is using a technology called mod_rewrite. Mod_rewrite is a simple technology that is used to rewrite URLs at the server level. Sound confusing? It really is not too difficult to understand. Simply put, mod_rewrite can make this url:
http.://www.domain.com/page.php?id=222&action=new
accessible to your users by linking to:
http.://www.domain.com/222/new/
You do not have to change your files around at all, just create a rule.
Using PHP and ASP to Redirect
For those users who are drastically changing their website’s structure, they may find that using mod_rewrite or a simple apache redirect just does not cover all their bases. An example of this could be a blog owner who decides to change the links to their posts from this:
http.://www.blog.com/archives/20010/8/9/
To this:
http.://www.blog.com/archives/my-post-title
It would be unrealistic to try and enter a redirect rule for every post that this person has made in the past, and the URL’s do not share any of the same information, which makes it impossible to use mod_rewrite alone. In a case like this, it is necessary to fall back on a script that will take the old URL, feed it to a script which in turn will provide the official redirection.
Both ASP and PHP have the ability to provide a 301 redirect to browsers. To redirect a browser or spider with PHP or ASP, you should use the following code:
PHP Redirect
<?
header(‘HTTP/1.1 301 Moved Permanently’);
header(‘Location: http://www.newdomain.com/newdir/newpage.htm’);
exit();
?>
Please REPLACE www.newdomain.com with your actual domain name.
ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.newdomain.com/newdir/newpage.asp”
%>
Please REPLACE www.newdomain.com with your actual domain name.
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>
ColdFusion Redirect
<.cfheader statuscode=”301″ statustext=”Moved permanently”>
<.cfheader name=”Location” value=”http://www.new-url.com”>
Redirect to www
Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc].
Please REPLACE www.domain.com with your actual domain name.
If you find yourself in this situation and are not familiar with programming, you should hire one to do this for you.