Knowledgebase

How to redirect content

In this article we'll show you how to redirect content using the Redirect directive. This is the simplest way to redirect content, and respectively the one that offers fewer options and less flexibility. The RedirectMatch directive, for example, can be used to set up more complex redirect rules. Both directives are associated with mod_alias; this is an Apache module that's installed on all HostKnox servers.

In its most basic form the directive should be followed by the path to the content which you want to redirect and then the destination URL. The path to the content that you want to redirect is relative to the public_html directory on your hosting account. The destination URL can be an external one (e.g. http://domain.com), or it can be just a path to a location on your hosting account (relative to public_html). So, for example, if you want to redirect the content from the whole public_html folder on your account to the external URL address http://yourdomain2.com, the directive would look like this:

#Redirect the public_html folder to an external URL
Redirect / http://yourdomain2.com

In the above example since the folder that you want to redirect is the root one (public_html), the path is just a forward slash. You need to put the rule in the .htaccess file that's in the public_html folder. If you don't know how to create or edit .htaccess files, check out the article on creating and editing .htaccess files.

Instead of redirecting to an external URL you can also redirect to another folder on your hosting account. In this case you need to specify the path to the folder in relation to the public_html directory. For example, you want to redirect the content from public_html/folder1 to public_html/folder2, then you need to use the following directive:

#Redirect public_html/folder1 to public_html/folder2 within the same account
Redirect /folder1 /folder2

In this case you can also put the rule in the .htaccess file that's in your public_html directory. You don't have to put the rule in an .htaccess file that's in public_html/folder1. You can use the .htaccess file in public_html no matter whether you want to redirect just a subfolder. Of course, it's possible to put the directive only in the .htaccess file that's in the particular subfolder that you want to redirect; the directive would be the same.

However, you should keep in mind that if, for example, the root directory (public_html) is redirected to an external URL and a subfolder on your hosting account is redirected to another subfolder on your account, the redirect rule for the subfolder will not work. If the subfolder is redirected to another external URL, there will be no problems. You should also remember that if you combine more than one redirect rule, the more specific one should be listed first. For example:

#The most specific redirect should be first, and the least specific last
Redirect /folder1 http://yourdomain3.com
Redirect / http://yourdomain2.com

will redirect public_html/folder1 (e.g. yourdomain.com/folder1) to yourdomain3.com, while the public_html folder will be redirected to yourdomain2.com. If you reverse the order of the directives, the second one (which is more specific in terms of the location on your hosting account) will not work.

Another thing worth mentioning is that if you redirect a folder, its whole content together with subfolders will be redirected to the destination URL. So, for example, if public_html (e.g. yourdomain.com) is redirected to yourdomain2.com and a client requests yourdomain.com/file.txt, the request will be redirected to the exact match of yourdomain2.com/file.txt. If no such file exists in the corresponding location for the second URL, then a 404 Not Found error will be returned to the browser of the client.

Of course, it's also possible to redirect just a single file. For example:

#Redirect a single file
Redirect /images/1.jpg http://yourdomain2.com/pictures/1.jpg 

The lines in the examples that begin with # are comments. You can put such comments in your .htaccess files; they don't affect the directives.

For information on the RedirectMatch directive read the article on redirecting content with RedirectMatch. The most complex redirects with various conditions can be achieved with mod_rewrite (an Apache module). For more information check out the tutorial on mod_rewrite and the mod_rewrite articles in our knowledgebase.

Was this answer helpful?

 Print this Article

Also Read