Using htaccess 301 redirect in folder or subdirectory
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
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., codegrad.hub.ph 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.