leafleafDocy banner shape 01Docy banner shape 02Flower illustration

How to Set Up a 301 Redirect in .htaccess

A permanent 301 redirect informs search engines and other online services that an old link has been replaced by a new one. It’s the recommended method for directing traffic from an existing page. This type of 301 redirect can be set up directly via the .htaccess file or in the cPanel interface, if available.

  • Common Uses of 301 Redirects
  • Set Up 301 Redirect via .htaccess file
  • Set Up 301 Redirect via cPanel

Common Uses of 301 Redirects

Some common uses of a 301 redirect in .htaccess:

  • Redirect individual files on the same domain.

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

  • 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.

  • Force www. version of a domain or force non www. version of a domain.

Visitors can access and link to your website through either example.com or www.example.com. With a redirect, you can set one of these as the preferred domain name that your site displays.

  • 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.

Set Up 301 Redirect via .htaccess file

On a Linux server you would use your .htaccess file to implement a 301 redirect for your pages. We’ll now show you how to edit the .htaccess file, then go over the different redirect options.

  1. Login to your cPanel.
  2. Under Files, click on File Manager.

   3. Ensure that Show Hidden Files is selected by clicking Settings on the top right corner of the File Manager.

   4. Select the Document Root for: option, and choose your domain from the drop-down.

   5. Right-click on the .htaccess file and select Edit.

   6. If your .htaccess file is still not found after the previous steps, click on New File at the top-left, name the file .htaccess, and set the directory for the file to be created to /public_html/ or the document root of your site.

   7. You might have a text editor encoding dialog box pop-up, you can simply click on Edit to proceed.

Redirect 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

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


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

Redirect domain

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

Sample of code from example.com domain’s .htaccess file:


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

Force www. on domain

A search engine such as Google interprets example.com and www.example.com as essentially two separate websites. It is recommended that users pick one version they’d like search engines to display. By using a 301 redirect, users can specify which domain name is displayed on their site.

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


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

Force non-www. on domain

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


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

Redirect files by 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]

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.

Set Up 301 Redirect in cPanel

For users that host their sites on cPanel-based hosts, it is possible to set up redirects via the cPanel interface. This tool automatically adds the necessary code to the .htaccess file for the redirect to function properly.

  1. Login to cPanel.
  2. Click the Redirects button in the Domains section.
  3.  You will then be on the Add Redirect page. Click the drop-down box for Type and choose either a Permanent (301) or Temporary (302) redirect.
  4. Click the next drop-down box and choose the domain you want to redirect.
  5. For the slash ‘/’ field, enter any folder names (if necessary).
  6. Enter the address you want to redirect to in the Redirects to section.
  7. Choose if you want to ‘Only redirect with www.’ ‘Redirect with or without www.’ or ‘Do Not Redirect www.’
  8. Check the box if you want to create a Wild Card Redirect. This will add the the file/folder name after the url when it redirects. For instance, example.com/test.php would redirect to example2.com/test.php.
  9. Click Add when finished.

Congratulations, now you know how to create a redirect in your cPanel! Did you know? If you have a WordPress site, you can also create a 301 redirect directly in your dashboard.

Know you’ll always be able to reach your customers when they need you with our Dedicated Hosting Services

Share this Doc

How to Set Up a 301 Redirect in .htaccess

Or copy link

Table Of Contents