When configuring Apache 2.2 as a reverse proxy for Tomcat applications, developers often need to handle specific URL redirects while maintaining the proxy functionality. The core issue arises when trying to mix Redirect
directives with ProxyPass
in the same configuration.
The original configuration:
ProxyPass / http://127.0.0.1:8090/ connectiontimeout=300 timeout=300
ProxyPassReverse / http://127.0.0.1:8090
Redirect permanent /broken/page.html https://www.servername.com/correct/page.html
fails because the ProxyPass /
directive catches all requests before the Redirect
can be processed. Apache processes these directives in order, and the catch-all proxy takes precedence.
The proper way to implement this is using Location
blocks:
<Location "/broken/page.html">
Redirect permanent /broken/page.html https://www.servername.com/correct/page.html
</Location>
<Location "/">
ProxyPass http://127.0.0.1:8090/ connectiontimeout=300 timeout=300
ProxyPassReverse http://127.0.0.1:8090/
</Location>
For more complex scenarios, mod_rewrite offers greater flexibility:
RewriteEngine On
RewriteRule ^/broken/page\.html$ https://www.servername.com/correct/page.html [R=301,L]
ProxyPass / http://127.0.0.1:8090/ connectiontimeout=300 timeout=300
ProxyPassReverse / http://127.0.0.1:8090
The [L]
flag ensures Apache stops processing rules after matching this one.
- Ensure
mod_proxy
,mod_proxy_http
, andmod_rewrite
(if used) are loaded - The order of directives matters - specific paths should come before catch-all proxies
- Always test with temporary redirects (302) before implementing permanent ones (301)
Check your configuration with:
apachectl configtest
tail -f /var/log/httpd/error_log
Increase log level temporarily for debugging:
LogLevel debug rewrite:trace6 proxy:trace6
When configuring Apache as a reverse proxy for Tomcat applications, developers often encounter scenarios where they need both proxying and redirect capabilities. The core challenge is that ProxyPass directives typically take precedence over Redirect directives in Apache's processing order.
Apache processes URL transformations in this order:
1. Alias and Redirect directives
2. ProxyPass directives
3. Filesystem handling
This means your Redirect directive might be getting overridden by the catch-all ProxyPass rule. Let's examine a working configuration example.
Here's how to properly combine both directives:
# Specific redirect first
Redirect permanent /broken/page.html https://www.servername.com/correct/page.html
# Then general proxy rules
ProxyPass /broken/page.html !
ProxyPass / http://127.0.0.1:8090/ connectiontimeout=300 timeout=300
ProxyPassReverse / http://127.0.0.1:8090
The exclamation mark (!) in ProxyPass /broken/page.html !
tells Apache to exclude this path from being proxied, allowing the Redirect to work. This is crucial for the solution.
For more complex scenarios, consider these approaches:
# Using Location blocks for better control
<Location "/broken/page.html">
Redirect permanent /broken/page.html https://www.servername.com/correct/page.html
</Location>
<Location "/">
ProxyPass http://127.0.0.1:8090/ connectiontimeout=300 timeout=300
ProxyPassReverse http://127.0.0.1:8090/
</Location>
If redirects still aren't working:
- Check Apache's error logs for redirect-related messages
- Verify the mod_rewrite module is loaded
- Test with temporary (302) redirects first
- Clear browser cache between tests
When implementing multiple redirects with ProxyPass:
- Place specific redirects before general proxy rules
- Minimize the number of redirect exceptions
- Consider using RewriteRule for complex URL transformations