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

Under maintenance; comment temporarily disabled.


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