Setting up a permanent 301 redirect via .htaccess

fonte:http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-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:

You have the ability to setup redirects for a domain in your cPanel interface one link at a time. You can also add the redirects manually by modifying your .htaccess file directly.

Getting to your .htaccess file

On a Linux server you would use your .htaccess file to implement a 301 redirect for your pages.

  • Login to your cPanel.
  • Under Files, click on File Manager.
  • Select the Document Root for: option, and choose your domain from the drop-down.
  • Ensure that Show Hidden Files is selected.
  • Then click Go.
file-manager-hidden-files
  • Right-click on the .htaccess file and select Edit.
file-manager-htaccess-edit
  • If your .htaccess file didn’t exist already during the previous step, click on New File at the top-left, name the file .htaccess, and finally set the directory for the file to be created to /public_html/ or the document root of your site.
file-manager-htaccess-create
  1. You might have a text editor encoding dialog box pop-up, you can simply click on Edit.

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

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

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

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

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.

How to Setup a Domain Redirect in cPanel

fonte: http://www.inmotionhosting.com/support/edu/cpanel/cpanel-manage-domains/redirect-domain

At times you may want to redirect traffic from one domain to another domain. This can be done at the domain level or for specific web pages. For example, domain.com and it’s web traffic can be redirected to anotherdomain.com OR domain.com/aboutus.html can be redirected to anotherdomain.com/about.html. In this guide we will show you how to create a redirect in your cPanel. This automatically adds a redirect rule to your .htaccess file for you.

Redirecting a Domain

    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 if you want to create 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 redirect. For instance, example.com/test.php would redirect to example2.com/test.php.
    9. Click the Add button when you are finished.


You are finished when you see a green message with the details of your redirect.

Congratulations, now you know how to create a redirect in your cPanel! You can also create a 301 redirect directly in your .htaccess file.