301 Permanent Redirect Through .htaccess Explanation

A 301 redirect is a permanent redirect of a URL or set-of-URLs to a new destination. This is often a necessity in SEO (search engine optimization) considerations. It is utilized by SEO professionals to manage link and maturity value that a page or domain may have earned over its lifetime.

Here, we will cover several types of 301 redirect that a webmaster or SEO may come across.

Info: This tutorial is specifically for Linux servers that utilize the .htaccess file for this purpose.

The first step is to locate the .htaccess file (usually in the root folder of your website) or create one if there is not any. To create an .htaccess file, open a new text file and name it accordingly. It may be necessary to give it a .txt (or other) extension for your operating system to allow it to be saved (some OS will not allow a file to start with a dot (.) The file will then have to be renamed to .htaccess after being uploaded to the server.

Now that the .htaccess file has been located (or created), here are the several options for doing 301 redirects. Each of the following directives should be used on its own line within the .htaccess file. Once the file is updated, it should be uploaded to the root directory of the server. It can then be tested to make sure that the redirect is working properly.

Note: We are assuming the current domain is example.com.

Redirect All Files with a Specific Extension

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Redirecting Individual Files

Redirecting one file to another on the same domain:

  • Redirect 301 /original-file-name.html /new-file-name.html

Redirecting one file to the same file name on another domain:

  • Redirect 301 /original-file-name.html http://www.example.net/original-file-name.html

Redirect an Entire Domain to Another

This will work on a Linux Server or macOS Server

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

Force www or the non-www Version of the Domain

Note: This helps consolidate the link value earned by a domain.

From non-www to www

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

From www to non-www

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

The flags

The flags at the end of each rule are defined as:

  • L (Last): If there is a match and the redirect is executed, no additional action will be taken.
  • NC (NoCase): It causes the rewrite process to be implemented in an insensitive manner.
  • R (Redirect): It causes an http redirect to be issued to the browser.
  • OR: It allows one of the rules to be applied (whichever has a match).
  • RW (Rewrites): are enabled by using the Apache module mod_rewrite, which is one of the most powerful Apache modules and features available.