Knowledgebase

How to redirect content within the same hosting account with mod_rewrite

One of the many things that you can do with mod_rewrite is to redirect files from one location on your hosting account to another location within the same account. You can do this with a few directives placed in an .htaccess file. For some basic information about the mod_rewrite directives that you can use check out the tutorial on mod_rewrite directives.

You can perform the redirection to another location on your hosting account externally or internally. The minimum that you need to redirect content with mod_rewrite is to turn on the rewriting engine with the RewriteEngine directive and to specify what is to be redirected with the RewriteRule directive. In case you want the redirect to be external you also have to add the R flag to the RewriteRule directive.

For example, let's say that you want requests for yourdomain.com/index.php to be redirected to yourdomain.com/folder/index2.php and you want the second URL address to be shown in the browser of the client, then you can place the following directives in the .htaccess file that's in the public_html directory on your hosting account:

#Redirect externally yourdomain.com/index.php to yourdomain.com/folder/index2.php
RewriteEngine On
RewriteRule ^index\.php$ /folder/index2.php [R]

Since in this example the base folder is not specified with the RewriteBase directive the path to the destination file (the Substitution argument) is relative to the root web-accessible directory on your hosting account (e.g. public_html). So the full path on the account would be public_html/folder/index2.php, but you only need to put /folder/index2.php in the RewriteRule directive. Another important thing to remember is that this path should always start with a forward slash when the redirection is external and the RewriteBase directive is not included. As we already mentioned, the R flag at the end of the directive makes it external.

With the RewriteCond directive you can set various conditions and restrictions on when the redirection should be performed. That's when mod_rewrite should be used instead of simpler solutions. For example, you can include conditions so that the content is redirected only when it's accessed from specific browsers (e.g. Mozilla, Internet Explorer), from particular IP addresses, from links from particular sites, etc. For some details on the RewriteCond directive check the tutorial on mod_rewrite directives.

The above example is a simple one that can also be performed with the Redirect or the RedirectMatch directives. For such simple tasks it's recommended and also easier to use one of those two directives. For more information on how to use them check out the article on redirecting content with the Redirect directive and the one on redirecting content with the RedirectMatch directive.

The internal redirection is performed in the same way but without the R flag at the end of the RewriteRule directive. What's interesting about the internal one is that you can use it if you want the URL address in the address bar of the client's browser to be that of the old content (the one that's being redirected). For example, if you want to redirect yourdomain.com/index.php to yourdomain.com/folder/index2.php and you want the clients to use and see the old URL yourdomain.com/index.php, then you can use an internal redirect:

#Redirect internally yourdomain.com/index.php to yourdomain.com/folder/index2.php
#with clients seeing the URL yourdomain.com/index.php in their browsers
RewriteEngine On
RewriteRule ^index\.php$ /folder/index2.php

Note that unlike the external redirect to content on the same account, with the internal one even if you haven't specified the RewriteBase, it will still work without the forward slash at the beginning of the destination path (e.g. folder/index2.php).

It's also possible to redirect content with mod_rewrite to another site by using the full URL address. For more information read the article on redirecting content to external URLs with mod_rewrite.

Was this answer helpful?

 Print this Article

Also Read