A permanent 301 redirect in your .htaccess file lets search engines and others know that a 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 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

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

  1. Login to your cPanel.
  2. Under Files, click on File Manager.
  3. Select the Document Root for: option, and choose your domain from the drop-down.
  4. Ensure that Show Hidden Files is selected.
  5. Then click Go.
  6. file-manager-hidden-files
     
  7. Right-click on the .htaccess file and select Edit.
  8. file-manager-htaccess-edit
     
  9. 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.
  10. file-manager-htaccess-create
     
  11. 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.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Comments

Post a comment
n/a Points
2014-05-03 11:01 am

Need a solution to this, not addressed above.  Want to redirect all "example.com/shop" to "www.example.com/shop" pages.  Our SSL certificate covers "www" pages on our shopping cart (i.e. "www.example.com" not "example.com").  On occasion, customers will try to go to a cart page without the "www" prefix and get a warning notice because certificate doesn't cover those pages.  Cart settings usually force the "www" but occasionally fail and/or need to be reset.  

Have tried to create a redirect, but keep getting "loop" errors.  Above discussion doesn't address this specifically.  Any help would be appreciated.

Staff
7,266 Points
2014-05-05 8:57 am
Hello Raplh,

Thank you for your question. We have a general guide on How to force www or non-www in htaccess.

What program are you using to build your website? Depending on your CMS, you may a have to accomplish this a different way. For example, in Wordpress, you would change the site URL from http://example.com to https://www.example.com.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-06-08 12:48 pm

Please provide me solution

 

I want to redirect my site from

http://bluemoonacres.net/in-our-markets/

to

http://bluemoonacres.com/in-our-markets/

For all my websites link

 

Please Please Please

Iam stucked

Staff
15,308 Points
2014-06-09 12:16 pm
Hello Pardeep,

If you are simply wanting to redirect the .net to the .com, you will want to follow the instructions for redirecting from old domain to a new domain. You can simply add the /in-our-markets/ to the end of the domain names to assist with the redirects.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/subfolder [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com/subfolder [NC]
RewriteRule ^(.*)$ http://example.net/subfolder/$1 [L,R=301,NC]


Kindest Regards,
Scott M
n/a Points
2014-06-13 8:19 am

Thanks, it helped me a lot.... I wanted to redirect all old links from asp pages to php pages, and the article "Redirect all files with certain extension" almost worked, except that it would redirect to the root site for links within subfolders...

for eg: i would like to redirect http://examplesite.ext/sub/page1.asp to http://example.ext/sub/page1.php

But the code shown here redirects to http://example.ext/page1.php

please help

Staff
7,372 Points
2014-06-13 9:15 am
The following code will allow you to redirect but keep the folder structure:

RedirectMatch 301 (.*)\.asp$ http://www.example.com$1.php

n/a Points
2014-06-18 10:51 pm

I want to move my site from a nation domain to .com domain, could you suggest me to do it. 

it's is www.lacda.vn and move to www.muhato.com

Staff
7,372 Points
2014-06-19 8:22 am
You would simply need to buy the new domain if you have not already, and add it to your cPanel as an addon domain or perform a primary domain change on your account. Depending on how your site is built, you may also need to make some additional configuration changes if anything is referencing the old domain directly.
n/a Points
2014-06-30 3:06 pm

Good afternoon,

I have create some subdomains for my basic domain. Because I have an SSL card only for principal store, I want that the all login page, registered page and sign-up page and check-out page from all sub-domains to be redirect on basic store. Some example of links are:

- https://mysite.com/login.html (login page from basic store)

- http://sub-domain-1.mysite.com/login.html  (login for sub-domain 1, corresponding to login page)

- http://sub-domain-1.mysite.com/?route=account/signup (signup for sub-domain 1, corresponding to signup page)

- http://sub-domain-1.mysite.com/create-account.html (register for sub-domain 1, corresponding to register page)

- https://mysite.com/checkout.html (check-out page from basic store);

 

Also, I observe that on my home page from basic store don´t appear https on browsers. How I can allow to appear https on home page, also using .httaccess file?

Thank you very much for your advice and support.

I waiting your advice.

Best regards.

P.S. I use OpenCart 1.5.6, multi-store (under the same admin, multiple stores)

Staff
15,484 Points
2014-06-30 4:19 pm
Hello Johnny P,

SSL certificates are made to work with a specific URL. So, if you order it to go to "https://mysite.com", then all sites in that URL should be secure. However, since you're using a different URL than what was specified for the SSL, the certificate does not protect it. The URL that you mentioned above (sub-domain-1.mysite.com/login.html ) will not work because it is not formatted for the SSL that was purchased. However, there is a way to use the subdomains in this way as long as they are using the primary domain.

If you were to use a Wildcard SSL (you would have to specify that during the purchase of the SSL), then you could use the subdomain format that you're using:

subdomain.mysite.com

However, if you're using 2 different URLs (you mentioned a principal store and a basic store), the SSL will not work that way. You would definitely need to purchase 2 different SSL certificates.

I hope that helps to clarify the answer. Please let us know if you require any further assistance.

Regards,
Arnel C.

n/a Points
2014-06-30 11:54 pm

Good morning,

Thank you for answer Arn.

I know this basic detail reffering to SSL Wildcard. Fortunately, OpenCart share the same details for all their sub-domains. This Is the reason because only I want to redirect some basic links (such I specified anterior). When somebody want to login on my some sub-domains, than automatic will be redirected on the corresponding page from basic store (is the same think as principal store, but in other words).

I can redirect those link using file 301 and htaccess? What is the command line which I can add it to ensure redirection? Also I don´t have in my ftp the 301 file.

Thank you for support and advice to all comunity.

I waiting your answer.

Best regards.

Staff
15,308 Points
2014-07-01 9:10 am
Hello JohnnyP,

If I am correct, you are asking if it is possible to redirect http://login.example.com to https://example.com/login. This can absolutely be done via the htaccess file, but it can be done even easier via the redirect tool for cPanel. This writes the .htaccess code for you. I have tested this on my own server and it worked perfectly.

Kindest Regards,
Scott M
n/a Points
2014-07-01 12:54 pm

Hello

I moved my site from ASP to WordPress an I need to redirect any .asp page to the site root. I tried with this code but it doesn't work

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

$nbsp;

Any help will be welcomed

Redards

Staff
9,521 Points
2014-07-01 2:58 pm
Hello Ciro,

You would more than likely want to insert your rewrite rule inside the default WordPress .htaccess rules like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.asp$ /index.php [R=301,L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Please let us know if that is working for you.

- Jacob
n/a Points
2014-07-02 4:32 pm

Thanks Jacob!

Staff
9,521 Points
2014-07-02 6:03 pm
Hello Ciro,

No problem at all, glad to help out!

Taking a closer look at this, it looks like I might have given those rules to you in haste. While it does seem to technically force all requests for a .asp page to your main WordPress page, it looks like it also possibly breaks the inclusion of CSS files and images.

So you would want to use the redirect code above your default WordPress rules like this instead:

RewriteEngine On
RewriteRule ^(.*)\.asp$ / [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Also as I've answered Heidy's question below about the SEO impact of directing old URLs to new ones. You might also want to think about directly setting up a 301 redirect if you've got the same content just in WordPress now.

Redirect 301 /helpful-page.asp /helpful-page


Please let us know if this works out for you!

- Jacob
n/a Points
2014-07-02 1:46 pm

Hello,

I appreciate very much your help to solve the following issue.  

There is already a live site created long ago in Dreamweaver with .htm and .html files. Recently, I created a completely new site with Joomla 2.5 in a subdirectory under the same root directory of the old site.  My intention now is:

1) Redirect all trafic to the subdirectory where I have the new joomla site, using the same domain.

2) I will keep the old site as it is, but I need to redirect old .html and .htm pages to the new corresponding pages in joomla 2.5 to avoid loosing ranking position in search engines.

3) I am concern that redirecting old pages to new pages would be a bad practice for search engines like Google.  I want to make sure that this will not be a problem, and if it is, then what it is the correct way for doing it.

I appreciate your help and explain how to do the above.

Thank you very much.

Staff
9,521 Points
2014-07-02 3:58 pm

Hello Heidy,

Luckily you can redirect your old .htm and .html pages to your new Joomla site, and won't lose any search engine ranking in the process when done correctly.

Search engines use the 301 redirect from a server to update their indexes to make sure they link to your fresh content. Search engines love it when you let them know a old helpful page can now be found at a new home. It helps their users get the most relevant and up to date content, and lets the search engine know you have someone maintaining your site and updating things.

Here is a great page from Google on 301 redirects and why you want to make sure to use it when updating your content. Hopefully that will help put you a little bit at rest for worrying about search engines not liking you updating your links.

Now do keep in mind from a SEO or Search Engine Optimization stand-point, if you're getting a lot of traffic to your HTML page about "how to use a PC". Then you go and completely change the content that was in your HTML page on the Joomla version, for instance you update it with tips on "how to use a Mac" and removed some of the PC information you had before. Over time you could lose ranking for the "how to use a PC" keyword phrase, because search engines care about relevant content and the reason people used to click on your page from Google results might not be there anymore.

To redirect all of your old HTML pages to your new Joomla subdirectory, you would simply edit your .htaccess file and use one of the following formats:

Redirect 301 /helpful-page.htm /joomla/helpful-page


Or include the full URL:

Redirect 301 /helpful-page.htm http://example.com/joomla/helpful-page


I hope that helps, and if you have any further questions at all, please let us know!

- Jacob
n/a Points
2014-07-02 4:10 pm

Perfect!  Thank you very much for your prompt response.  

Staff
9,521 Points
2014-07-02 4:29 pm
No problem Heidy, thanks for stopping by and commenting on my guide :)
n/a Points
2014-07-07 10:35 am

Hi!

How can i redirect joomla subdirectory to article ? Like mypage.com/subdir -> mypage.com/index.php/article=1

Staff
15,308 Points
2014-07-07 10:49 am
Hello Punkel,

The best way to do that would be to use the cPanel redirect tool. It will write the code for you and place it into your .htaccess file.

Kindest Regards,
Scott M
n/a Points
2014-07-07 3:36 pm

Thank you, but my site does not have cPanel. Can you paste here .htaccess example?

Staff
9,521 Points
2014-07-07 3:50 pm
Hello Punkel,

You would simply add this towards the top of your .htaccess file:

Redirect 301 /subdir /index.php/article=1



However further down the file, you'll need something that handles the rewrite of that /index.php/article=1 to actually do something. As article=1 is not a valid URL when just used like that as it would essentially be the same as requesting /folder-name=1 and most of the time you aren't going to have a folder or file with a = symbol in it.

Typically you're going the reverse where you want to go from a URL like /index.php?article=1 and make it a search engine friendly URL like just /page-name.

- Jacob
n/a Points
2014-07-08 1:59 am

Thank you a lot, this Redirect 301 /subdir /index.php/article=1

works perfectly. I use this redirect only for search bots.

Staff
9,521 Points
2014-07-08 2:10 am
Hello Punkel,

Awesome, glad that helped! It's still a little odd to see a search friendly URL being converted back to a numbering system. So if you happen to be trying to block bad bots for some reason with this redirect, you would find my guide on block users with .htaccess helpful, or even how to identify bad bots.

I hope you're not having a malicious bot issue, and just need to redirect to that URL format for some other reason instead. If so, again I'm glad it helped!

- Jacob
n/a Points
2014-07-08 1:26 am

Hello,

Good Morning

My Site is single page site that's and my Domain is http://example.com/#!/ and i have to convert this domain to http://example.com using 301 redirect then which code i have to use?

Staff
9,521 Points
2014-07-08 3:00 am
Hello Rinku,

You wouldn't be able to use a 301 redirect, or to my knowledge any RewriteRule rule to be able to alter the URL like that.

The pound symbol # doesn't actually go to the server for processing, it is all handled by the front-end. You can confirm that if you have access to your website's access logs and look at the request. So either the browser interprets it as an anchor link and navigates the user to a specific spot on the page. Or JavaScript grabs it and interacts with the browser's DOM in some fashion.

If you hit Ctrl-U in your web-browser you can see the HTML of your page, and you should notice this on the 4th line:

!-- Mirrored from example.com/index.html by HTTrack Website Copier/3.x



So it looks like this one page website might have been copied from another server, and in doing so, looks like it retained a bunch of links including #! already for switching to multiple pages of info when a link is clicked.

It looks like your /js/switcher.js file is responsible for this:

defHash:'#!/',
emptyHash:'#',



So it looks like this switcher JavaScript file is causing the #! part of the URL to show up in the browser more than likely. But the web page is using it for navigation, so if you simply tried to remove it, it would also remove the functionality of your links pulling up the different pages of the one HTML page.

Hope that helps, please let us know if you had any further questions.

- Jacob
n/a Points
2014-07-16 4:20 am

My website opening with "www" and without "www"  -- I want my website always open without "www" if some one open with "www" it should be redirect in without "www" - please inform me - how can i solve this problem - website made in PHP . thanks

 

Staff
9,521 Points
2014-07-21 7:21 pm
Hello Shashi,

You can use the .htaccess rules mentioned above from the force non www example in this guide:

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



This will force all requests for www.example.com to instead just redirect to example.com without the preceding www at the beginning of the domain.

Please let us know if you needed anything else at all.

- Jacob

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

News / Announcements

WordPress wp-login.php brute force attack
Updated 2014-07-17 06:43 pm EST
Hits: 201633

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!