Apache Proxy URL Redirection: Mod_Proxy and RewriteRule Implementation Guide


2 views

When working with Apache's proxy module (mod_proxy), URL redirection requires careful configuration of both proxy directives and rewrite rules. The key modules you'll need are:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so

Here's a fundamental configuration to redirect requests from olddomain.com to newdomain.com:

<VirtualHost *:80>
    ServerName www.olddomain.com
    
    ProxyRequests Off
    ProxyPreserveHost On
    
    RewriteEngine On
    RewriteRule ^(.*)$ http://www.newdomain.com$1 [P,L]
    
    <Proxy *>
        Require all granted
    </Proxy>
</VirtualHost>

For more complex redirection needs, you can combine multiple rewrite conditions:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/products/
RewriteRule ^(.*)$ http://newdomain.com/catalog/$1 [P,L]

When working with SSL endpoints, additional configuration is required:

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/key.pem
    
    ServerName secure.olddomain.com
    
    SSLProxyEngine On
    RewriteEngine On
    RewriteRule ^(.*)$ https://secure.newdomain.com$1 [P,L]
</VirtualHost>

When troubleshooting proxy redirects, check these common issues:

  • Ensure mod_proxy and mod_rewrite are enabled
  • Verify ProxyPreserveHost is set correctly for your use case
  • Check Apache error logs for rewrite rule processing details
  • Test with curl -v to see full request/response headers

For high-traffic implementations:

ProxyPass / http://newdomain.com/ connectiontimeout=5 timeout=30
ProxyPassReverse / http://newdomain.com/

This alternative syntax can be more efficient than rewrite rules for simple redirects.


When working with Apache's proxy module, many developers struggle with the reverse proxy scenario where incoming requests need to be transparently redirected to different domains while maintaining the original URL structure for clients. This differs from standard URL rewriting as it requires maintaining the proxy chain.

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so

The simplest implementation uses ProxyPass:

<VirtualHost *:80>
    ServerName www.olddomain.com
    ProxyPass / http://www.newdomain.com/
    ProxyPassReverse / http://www.newdomain.com/
</VirtualHost>

For more complex routing scenarios combining mod_rewrite with proxy:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [P,L]
ProxyPassReverse / http://newdomain.com/

Critical for maintaining session integrity:

ProxyPreserveHost On
ProxyRequests Off
RequestHeader set X-Forwarded-Proto "https"

Enable logging to troubleshoot redirect issues:

LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/proxy_error.log
CustomLog ${APACHE_LOG_DIR}/proxy_access.log combined

For secure proxy configurations:

<VirtualHost *:443>
    SSLEngine on
    SSLProxyEngine on
    ProxyPass / https://secure.newdomain.com/
    ProxyPassReverse / https://secure.newdomain.com/
</VirtualHost>

Add these directives to optimize proxy throughput:

ProxyTimeout 300
ProxyBadHeader Ignore
ProxyIOBufferSize 8192

Remember to test configurations with apachectl configtest before applying changes to production environments.