Using htaccess 301 redirect in folder or subdirectory

Last updated on February 11, 2013. Tags: , , ,

Setting up 301 redirect from one domain to another is a very easy task considering that all you need is to place a single line of code in the .htaccess file in the root directory of your old domain, but how about if you intend to redirect from a subdirectory (or folder) to a root directory, or from one subdirectory to another?

Let's say you initially availed and used it for several website projects that you have in mind such as, and Note that these are different websites because they have different topics and different appearance. Later on, when your business grow, you decided to moved these websites to, and so on.

How are you going to setup redirect in this scenario?

In the following sections, we shall analyze the behavior of .htaccess 301 redirect that involves subdirectories.

Putting the .htaccess in a subdirectory

Suppose you place the .htaccess file in and encode something like this in it:

redirect 301 /

You will be redirected to and not to The redirection works in a way that it looks for the subdirectory in the destination domain that is the same as the one containing the .htacess file.

If there is an existing and you typed, you will be redirected to the former. If there is no page.htm, you will be redirected to a "Not found" page but the address bar will still display

If you have 2 or more levels of subdirectory, such as putting the same code above in, you will be redirected to If such folder does not exist, you will be redirected to a "Not found" page.

Inverting the above scenario, putting 301 redirect in the root directory (of let's say and specifying a subdirectory such as:

redirect 301 /

...will actually redirect visitors trying to access to If there is an existing, accessing will automatically redirect the visitor to the former.

Put the .htaccess in root directory but indicate the subdirectory

In this method, instead of putting a separate .htaccess in the subdirectory, we will instead use the one in the root directory (or create one in the root directory in case you don't have one). In this case, we will indicate the subdirectory in the 301 redirect code.

Using the same example as in the previous section, if you have a website formerly located at and would like to redirect the visitors (as well as backlinks and page rank) to its new location at, open the .htaccess file in the root directory of and insert the following:

redirect 301 /folder/

You can use this method to redirect several subdirectories to new domains. Let's take our earlier example: in redirecting the subdirectories /website1/, /website2/ and /website3/ of to separate domains, and, put the following codes in the .htaccess of

redirect 301 /website1/
redirect 301 /website2/
redirect 301 /website3/

You can also use this method to redirect to a subdirectory of another domain:

redirect 301 /folder/

You can also use the above code to redirect from one subdirectory to another in the same domain. For example, you want to redirect incoming traffic from /folder/ to /folder2/, both subdirectories in You can put the following codes in the .htaccess of the root directory.

redirect 301 /folder/

Note that you still need to indicate the domain name even if the .htaccess is located in that domain.

This method does not work if you set the subdirectory as a subdomain. If you need to setup 301 redirect for subdomains, visit this article: Using htaccess 301 redirect in subdomains.

Posted by Greten on April 22, 2009 under Server Configuration

Read Comments
DimitriAu says:

How do I redirect to mydomain/myfolder/ ?

Greten says:

You can try using this: Pointing the domain to a folder within the root directory though as you can see in my notes at the bottom, I still need to further study that tweak.

Re:301 redirects from the subfolder:

This is a newbie question, so I hope you will bear with me. Do I need to put anything else in the .htaccess file besides the one line redirect?

I tried doing the first method you mentioned, and I can’t get it to redirect at all.

There is an .htaccess file in the root directory of my site, too, but I don’t want to load that root level .htaccess file up with a bunch of redirects. I have about 25 subfolders with about thirty redirects in each subfolder, so putting 600 redirects in the root folder would end up slowing down every page.

Is there something I should look for in my root folder which might be “overriding” the redirect in the subfolder?

Thanks in advance for your help (and thanks for posting the article in the first place.

Greten says:

Hi Srisuda

It seems that you misunderstood this post. There are no first and second methods. In the section “Putting the .htaccess in a subdirectory”, I was merely explaining what happen when you put the redirect code in the .htaccess in a folder. There is only one method, and it is by putting the redirect code in the .htaccess of the root directory. I’m afraid you really have to clutter your .htaccess file if you have that too many pages (600 you say?) to redirect. I’m not sure to what extent does it slow down the site since I never had 600 redirects before.

dffg says:

Thanks mate!
Was trying a long time to redirect from subdirectory to root but failed all the time. Now I know why, placed it in root and it works.. thanks!

Greten says:

You’re welcome

Robert says:

Hi can I ask you a question? I’m doing redirects on several of my websites but on one it doesnt seem to work. This is my code:

Redirect 301 /subfolder1/subfolder2/postname/ http://www.domain.ext/postname/

I still get a 404 when I click the old url instead of being forwarded to the new url.
Can you see spot anything wrong in the notation of the code?

Greten says:

Hi Robert,

Sorry for this very late reply. So busy with real life work.

Try removing the “postname/” part. This code is for redirect folders and not for redirecting specific files. :)

Share your thoughts

Comment Rules and Reminders

  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field should be your actual name, part of your actual name, or your nickname, and should only be four words at maximum
  • No links in the body of the comment itself. If referring to an external source is essential, please type the URL with space between each character e.g., becomes c o d e g r a d . h u b . p h. Do not hard code the URL as HTML link.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.

HTML, CSS, Javascript, search engine optimization, WordPress customization and other web development tutorials by Greten Estella