Setting up a 301 permanent redirect via .htaccess
Written by Jacob NicholsonIn this article we'll discuss using the .htaccess file which can control Apache's mod_rewrite module to help rewrite and redirect URLs on the server. We'll focus specifically on what are called 301 permanent redirects in this guide.
A 301 permanent redirect is a way to tell search engines on the Internet that one URL has been replaced permanently by another. A common need for a 301 permanent redirect is if you're switching domain names, or changing linking structures. You want to make sure that all of the backlinks and traffic you've earned on the old URLs don't suddenly disappear once making the change.
You can setup redirects for a domain in your cPanel interface one at a time, or following the steps below you can modify your .htaccess file directly to add these manually yourself.
Getting to your .htaccess file
- Login to your cPanel.
- Under the Files section, 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.
- Right-click on the .htaccess file and select 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.
- You might have a text editor encoding dialog box pop-up, you can simply click on Edit.
Making your .htaccess edits
- With the .htaccess file opened for editing you can now begin to type in your redirect code.
To 301 permanently redirect an entire domain you could simply enter in the following format:
Redirect 301 / http://NewDomain.comThis would cause typing in OldDomain.com/path/to/file.htm to load up in your web-browser as NewDomain.com/path/to/file.htm.
Or if you'd like to just redirect individual files you could set those up to either point to new files on the same domain, or ones on a completely different domain as well using the following format:
Redirect 301 /oldfile.htm /newfile.htm Redirect 301 /oldfile2.htm http://NewDomain.com/newfile2.htm
This would cause typing in OldDomain.com/oldfile.htm to load up in your web-browser as OldDomain.com/newfile.htm, and typing in OldDomain.com/oldfile2.htm would bring you to NewDomain.com/newfile2.htm
Finally you can also use the more advanced RewriteCond condition statements and RewriteRule rules if you needed to redirect more dynamic content. In this next code snippet for instance, we are redirecting any URL that ends with .htm and rewriting that to the same request but ending in a .html extension instead.
RewriteEngine On RewriteCond %{REQUEST_URI} .htm$ RewriteRule ^(.*).htm$ /$1.html [R=301,L]This would cause typing in OldDomain.com/oldfile.htm to load up in your web-browser as OldDomain.com/oldfile.html.
- Once you are finally done making your edits to the .htaccess file, you can then click on Save Changes to save your edits.
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.
Latest Questions
Need more Help?
Search
Ask the Community!
Current Customers
| Chat: | Click to Chat Now | E-mail: | support@InMotionHosting.com |
|---|---|---|---|
| Call: | 888-321-HOST (4678) | Ticket: | Submit a Support Ticket |




