Apache URL Redirection on VPS

I faced an issue with redirecting a particular URL to another URL at the Apache settings. You can write an index.php(php based solution) file for redirecting the header to the required URL. However, this didnt seem to me like an apt solution hence I looked for a solution at the Apache Config itself.

Considering you have a VPS where you have configured the virtual host as follows:

<VirtualHost *:80>
    ServerName pr.example.com
    ServerAlias pr.example.com
    DocumentRoot /sites/www/pr.example.com/public_html/
    ErrorLog /sites/www/pr.example.com/logs/error.log
    CustomLog /sites/www/pr.example.com/logs/access.log combined
</VirtualHost>

To redirect URL using Apache, you need to have mod_rewrite module enabled.

The Apache module mod_rewrite is a really sophisticated module which provides a powerful way to do URL manipulations. It allows complex manipulations but with added complexity it becomes difficult for a beginner to understand this manipulation techniques.

mod_rewrite is used for creating user friendly URLs.
For example:

can be converted as :

http://pr.example.com/drupal/user/1
[/codefilter_code]

The solution can be implemented by adding the following line at the before the ending VirtualHost tag. The implementation is to change the URL from pr.example.com to kb.example.com.
RewriteRule ^/(.*)         http://kb.example.com/$1 [L,R]

Doing this is sufficient for Virtual Host shown above since the match for the required pr.example.com is already done. Hence, the only change required is for writing the final rewrite conditions for the match.

mod_rewrite is mostly enabled be default in Apache. For making rewriting work on your server , ensure that the following line is mentioned in apache2.conf file or individual Virtual Host File

RewriteEngine on

--
Relevant Sources
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html