What Is SEO?    A Holiander Production
Request Guidance inquiries@whatisseo.com
WiSEO / SEO Guide / 301 Permanent Redirect Through .htaccess Tutorial

301 Permanent Redirect Through .htaccess Tutorial

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 and utilized by SEO professionals to manage link and maturity value which a page or domain may have earned over its lifetime.

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

Note: This tutorial is specifically for Linux servers, which 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 one present. To create an .htaccess file, create a new text file and name it accordingly. It may be necessary to name 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 the redirect is working properly.

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

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

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

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]

Redirect All Files with a Specific Extension

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

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

The Best Resource for SEO Shoppers