Last Updated on April 16, 2021 by Amit

Apache Proxy module

Reditect domain to another domain with P flag htaccess

“Apache Proxy module” also known as mod-proxy is a URL rewriting module provided by Apache. This module enables URL rewriting between domains hosted on two different servers.
With mod-proxy enabled , you can redirect your domain to an external domain without changing the URL in browser.

You can proxy requests from your domain to a domain on remote host with this module. You can learn more about mod-proxy on the official Apache guide which is on the following URL https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

This module doesn’t come pre-installed which means you must first enable it from the server config in order to use it.

How to use mod-proxy?

You can use proxy module to rewrite external URLs. If this module is active on your server then you can use mod-rewite P flag which stands for “Proxy” .
P flag can be used in htaccess, server.config and virtual host context.
With P flag you can rewrite your URLs to URLs on another domain which is being hosted on a remote server.
For example:
You can rewrite




P flag will proxy the request from domain1 to domain2 without changing the URL.

Using P flag in htaccess

P flag can be used in htaccess or other contexts. Here I will show you an htaccess example so you can understand how it actually works.

RewriteEngine on

RewriteRule ^foobar http://example.com/foobar [P]

To see how it works, put the above code in your /.htaccess and visit yoursite.com/foobar .
The rule will internally map your URL to example.com/foobar showing you the contents of the example.com domain on the same URL.
In case the rule returns a 500 server error then one possible might be that the module isn’t enabled.

How to install mod-proxy on Apache server

To install the proxy module
Go to the source directory of the Apache installation

cd /usr/src/httpd-2.2.24

Replace the path with where you downloaded the Apache installation.
Reconfigure the existing apache configuring appending the mod_proxy lines
./config-nice –enable-proxy=shared Rebuild Apache:
Install the new Apache
make install
Once you have done that you will want to edit the Apache Configuration to load the modules

vim /etc/httpd/conf/httpd.conf
Add the following modules and save the file

LoadModule proxy_module lib/apache/mod_proxy.so
LoadModule proxy_http_module lib/apache/mod_proxy_http.so
LoadModule proxy_balancer_module lib/apache/mod_proxy_balancer.so
LoadModule proxy_connect_module lib/apache/mod_proxy_connect.so
LoadModule proxy_ftp_module lib/apache/mod_proxy_ftp.so
LoadModule proxy_scgi_module lib/apache/mod_proxy_scgi.so
LoadModule proxy_ajp_module lib/apache/mod_proxy_ajp.so

Restart Apache to load them using service httpd restart
In case you don’t have access to the main configuration file (this usually happens if you are on a shared hosting server) then you can ask your hosting provider to enable the module.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.