Setting up a permanent 301 redirect via .htaccess

A permanent 301 redirect in your .htaccess file lets search engines and others know that an old link has been replaced by a new one. It's the recommended method for directing traffic from an existing page.

Some common uses of a 301 .htaccess redirect:

  • Option 1: Redirect individual files on the same domain

    An old file has moved locations, or the information is now contained in a new file.

     

  • Option 2: Redirect an old domain to a new domain

    You've moved a website from an old domain to a new one, and you want any old links to go to the new site.

     

  • Option 3: Force www. version of domain to be used
    Visitors access and link to your website in multiple ways such as example.com, and www.example.com and you can set one as the preferred method that your site displays.

  • Option 4: Force non www. version of domain to be used
    Visitors access and link to your website in multiple ways such as www.example.com, and example.com and you can set one as the preferred method that your site displays.

  • Option 5: Redirect all files with certain extension
    You used to have all of your files using an extension like .php and have converted everything to .htm so using a 301 redirect you can update all links to use the new extension.

In the File Manager of the control panel, you can edit or create the .htaccess to add these rules into.

 

 Option 1: Redirect individual files

To redirect individual files, like example.com/oldfile.htm to newfile.htm you can use a 301 redirect like this:

Redirect 301 /oldfile.htm /newfile.htm

single file 301 redirect

To redirect one specific file to another domain such as example.com/oldfile.htm to example.net/newfile.htm:

Redirect 301 /oldfile.htm http://example.net/newfile.htm

single file domain 301 redirect

 

Option 2: Redirect an old domain to a new domain

If you had an old domain such as example.com, and now you decided you actually want to use example.net for the website. You could setup a 301 redirect for the entire domain, so that old links to example.com carry over.

Code in the example.com domain's .htaccess file:

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]

full domain 301 redirect

 

Option 3: Force www. version of domain to be used

A search engine like Google would see example.com and www.example.com as essentially two separate websites. They recommend you pick one version you'd like search engines to display and using a 301 redirect is a possible option.

If you have a lot of links on the web where people are linking to your site as example.com, but you would like your visitors to instead end up at www.example.com you can force this version of your domain with these rules:

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

force www 301 redirect

 

Option 4: Force non www. version of domain to be used

If you have a lot of links on the web where people are linking to your site as www.example.com, but you would like your visitors to instead end up at example.com you can force this version of your domain with these rules:

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

force non www 301 redirect

 

Option 5: Redirect all files with certain extension

To re-direct all of one type of file to another, such as example.com/file.php to example.com/file.htm

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

file extension 301 redirect

You should now know how to properly setup 301 permanent redirects on your website to help ensure that search engines and visitors coming to your site from older links can still get to your new content.

Was this answer helpful?

 Print this Article

Also Read

Using ASP

Active Server Pages (ASP) is Microsoft's version of PHP for Windows. It is an embedded-HTML,...

How To Enable SSL on Your WordPress Site

Before you Begin you Need the Following: An SSL Certificate has been purchased, installed and...

When Your Web Site Won't Load

There are several reasons why your Web site may not load. To determine the problem, do the...

Why do I have a 500 Error?

By default, our Windows hosting accounts display a generic error when applications generate an...

Using .NET Core

LFC Hosting fully supports Microsoft .NET Core 2.To enable .NET Core you need to configuration an...