301 redirects using htaccess

301 redirect htaccess tutorial
301 redirect htaccess tutorial

In this tutorial we look implementing 301 redirects using htaccess and cover the following:


Before we go any further lets cover a few important points to bare in mind before starting:

  • htacccess is a configuration file for Linux Apache servers and not traditonally available on an IIS/Windows server (using Windows/IIS?)
  • The Apache Mod-Rewrite moduled must be enabled – uncomment LoadModule rewrite_module modules/mod_rewrite.so in your httpd.conf
  • RewriteEngine must be declared On in your htaccess file, we’ll come to this

What do we mean by 301?

301 is the status code returned by the server. When a 301 response is returned this means the requested resource/page has been permanently moved to a new location.

There are other redirects such as 302 or 307 redirects which are also known as temporary redirects. In all the programming languages or configuration utilities i’ve come across  302 redirect has been the default so to make it a permenant  redirect you have to explicitly defined this.

RewriteEngine on

Before we write any code we need to make sure the RewriteEngine is on. The following code does this:

RewriteEngine on

Quick & Simple redirects

Here are 2 methods of writing a simple permenant redirect in htaccess. This will redirect www.domain.com/old to www.domain.com/new. Note, it doesn’t have to be a directory, it could also be old.html and new html

Redirect permanent /old http://www.domain.com/new

or

Redirect 301 /old http://www.domain.com/new

I find either of these two methods ideal if you need to redirect a small number of pages as they are easy to remember and straight forward to setup.

Redirects using RewriteRule

The following example uses a RewriteRule and a regular expression to map ALL the pages/files of an old domain to a new one. Note the [R=301], this sets the redirect as a 301 response.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Although regular expressions can be tricky the result is worth the effort – in this line of code we have correctly redirected and mapped the entire website to its new domain.

RewriteConditions

Its not always the case you need everything redirected, sometime you need to be selective in your redirectionsg. Htaccess provides this flexibility through it’s RewriteCond directive.

Here are a few examples of RewriteCond in action:

Redirecting nonwww to www

Redirects all nonwww requests to the relevant www version.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

This rule says ‘redirect (301) all requests that are made under the nonwww to their relevant www version but ignore this if the request is under the www’.

Redirecting http to https

Redirects all http requests to the relevant https page by using port 443 as a condition.

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.example.com/$1 [R=301]

What if I’m hosted on a Windows/IIS Server?

If you’re using an IIS / Windows server you won’t be able to use the code below to perform your redirects. Instead, you will need to make the changes directly through the IIS interface, use a code alternative or look into using an IIS module that allows you to simulate htacces. ISAPI_rewrite is a popular alternative, visit http://www.isapirewrite.com/ for more info.

Here is a a good 301 redirect tutorial that shows how to set this up in IIS.

Useful Resources


One Comments

Leave a Reply