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 www.domain.com and used it for several website projects that you have in mind such as www.domain.com/website1/, www.domain.com/website2/ and www.domain.com/website3/. 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 www.website1.com, www.website2.com 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 www.domain1.com/folder/ and encode something like this in it:

redirect 301 / http://www.domain2.com/

You will be redirected to www.domain2.com/folder/ and not to www.domain2.com/. 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 www.domain2.com/folder/page.htm and you typed www.domain1.com/folder/page.htm, 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 www.domain2.com/folder/page.htm.

If you have 2 or more levels of subdirectory, such as putting the same code above in www.domain1.com/folder/folder2/, you will be redirected to www.domain2.com/folder/folder2/. 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 www.domain1.com) and specifying a subdirectory such as:

redirect 301 / http://www.domain2.com/folder/

...will actually redirect visitors trying to access www.domain1.com/ to www.domain2.ph/folder/. If there is an existing www.domain2.ph/folder/page.htm, accessing www.domain1.com/page.htm 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 www.domain.com/folder/ and would like to redirect the visitors (as well as backlinks and page rank) to its new location at www.domain2.com, open the .htaccess file in the root directory of www.domain.com and insert the following:

redirect 301 /folder/ http://www.domain2.com/

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 www.domain.com to separate domains www.website1.com, www.website2.com and www.website3.com, put the following codes in the .htaccess of www.domain.com.

redirect 301 /website1/ http://www.website1.com/
redirect 301 /website2/ http://www.website2.com/
redirect 301 /website3/ http://www.website3.com/

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

redirect 301 /folder/ http://www.domain2.com/folder2/

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 www.domain.com. You can put the following codes in the .htaccess of the root directory.

redirect 301 /folder/ http://www.domain.com/folder2/

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.

Codegrad

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