Internal redirect to external URL in Apache

If you want Apache to forward your requests to an external site when you acces a certain URL, you can configure a reverse proxy with mod_proxy.

First of all, you need to enable mod_proxy and mod_proxy_http.

a2enmod mod_proxy
a2enmod mod_proxy_http

Then, if you want all requests to /test to go to www.example.com, you would add the following lines in your site configuration, usually stored in /etc/apache2/sites-available/default:

ProxyRequests Off
<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>
ProxyPass /test http://www.example.com/

Now all requests to yourdomain/test will be forwarded in the background to http://www.example.com/.

You could do the same if you wanted to redirect all requests to a certain site somewhere else. For example, you could have the following scenario:

In this case, you can create a new site on the public Apache server that will redirect everything to the separate internal server.

As an example, let's say we have the domain example.com and we want every request that comes to example.com to go to our internal server at 192.168.0.123.

First, create a new site that will make the internal redirects. If you want to call this site example, you will usually need to create an /etc/apache2/sites-available/example file in which to write the configuration.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://192.168.0.123/
</VirtualHost>

Then, enable the site and restart Apache.

a2ensite example
/etc/init.d/apache2 restart