Setting up permanent 301 Redirects in htaccess

A 301 redirect is the Google recommended way to change the URL of a page showing in organic search results. If implemented correctly this can prevent your site from losing traffic from any broken links displayed in your Search Console.

One of the ways in which you can implement 301 status codes is through the use of your .htaccess file, but several Content Management Systems (CMS) that have their own built in functions or extensions, plugins or modules to take care of this for you.

These functions write commands in the .htaccess file but do so by offer you a far more user friendly interface to work with as opposed to having to access the raw file via the File Transfer Protocol (FTP).

What is an .htaccess file and what can I do with it? Supported by most web servers, your .htaccess or hypertext access file is exactly as the name suggests – it allows you to control which users can access your site allowing you to enter password restrictions and more. However, it can also be used to override and configure your site settings much like we will be doing with the 301 redirect.

Please be very careful when editing this file, ensuring you save a backup to your PC before making any changes as even a minor misplaced character can cause your site to crash, so make sure everything you do is reversible.

If your website doesn’t have a CMS or has one that doesn’t offer any 301 redirect functions then unfortunately you’re going to have to write them in your .htaccess file manually. The easiest way in which to access this is through a FTP program – I personally suggest FileZilla a free and easy to use client.

Once you have that downloaded you will need to get your sites address which will be in the form of an Internet Protocol (IP), and a username and password which can all be set up through your website’s cPanel. If you’re struggling to find your website IP you can do so by using free internet tools like Site24x7, you can also use this step by step guide on how to access your site FTP through Filezilla.

Once you have successfully accessed your site you will need to locate the file. Typically, this is located under the “/” folder – on the left hand side you will see your computer documents and on the right your raw website files. Once you have found the file you are looking for, right click and select “View/Edit” in Notepad/Wordpad and follow the steps below.

Individual 301 redirects

Implementing individual URLs is fairly simple.  Take for example if I wanted to redirect this page to edit.co.uk/blog/ I would write this on a new line:

Redirect 301 /setting-up-permanent-301-redirects-in-htacces/ /blog/
 
 

Redirecting an entire domain

Let’s say I have purchased both the .com and .co.uk domains, and wanted to redirect the .com site to my .co.uk site. I would do this by accessing the FTP for my .com domain and input this through writing a rewrite condition. Here is an example:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^edit.co.uk [NC,OR]

RewriteCond %{HTTP_HOST} ^www.edit.co.uk [NC]

RewriteRule ^(.*)$ http://branded3.co.uk/$1 [L,R=301,NC]
 
 

This rule would mean that the Edit.com URL would redirect to Edit.co.uk.

Forcing certain protocols

You may find that your site has duplicate pages when you’re running crawls which can be due to two versions of pages being indexed  for example www.edit.co.uk and edit.co.uk or potentially due to a trailing “/” at the end of the URL.

You can avoid this issue completely by forcing your website to use a certain version of the domain, you do this by adding a rewrite rule in the .htaccess, and here is an example:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.edit.co.uk [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
 
 

This would stop Google indexing any www. and apply a trailing slash to all URLs, preventing any potential duplicate pages.

Using your .htaccess file to make changes directly has both its advantages and disadvantages just like anything else. Some of the advantages of redirecting this way is that changes are immediate and the .htaccess is read on every server request, so there is no chance of Google indexing URLs you don’t want to be indexed.

The disadvantage of using this method to redirect your site is that it can cause a slight performance loss, as every {HTTP} request you make inside of the document is an additional file that needs to be accessed, but there are methods of avoiding this.

HTAccess is a powerful file where you can control a lot of how your website acts and performs due to it being a directory level document.

There are several things you can look to change in your HTAccess file in order to benefit your site’s SEO, including converting dynamic link URLs into a better looking and ranking structure.

You can also redirect index.php to the root or any .php based links for that matter, and also redirect www. to non www. There are also several more nice little tips and tricks, in which I will touch upon in follow up posts.

Related Posts