Apache Reverse Proxy Configuration: Handling Trailing Slash in ProxyPass Directives


3 views

When configuring reverse proxies in Apache, the trailing slash behavior often causes unexpected issues. Many developers encounter situations where /path works perfectly but /path/ returns 404 errors or fails to proxy correctly.

Your current configuration:

ProxyPass /test http://othersite.com/test
ProxyPassReverse /test http://othersite.com/test

This works for example.com/test but fails when users access example.com/test/. The core issue lies in how Apache matches and processes these URLs differently.

To handle both trailing slash and non-trailing slash scenarios, you need to implement either of these approaches:

Option 1: Dual ProxyPass Directives

ProxyPass /test http://othersite.com/test
ProxyPassReverse /test http://othersite.com/test
ProxyPass /test/ http://othersite.com/test/
ProxyPassReverse /test/ http://othersite.com/test/

Option 2: Using LocationMatch (More Flexible)

<LocationMatch "^/test/?$">
    ProxyPass http://othersite.com/test
    ProxyPassReverse http://othersite.com/test
</LocationMatch>
  • Ensure mod_proxy and mod_proxy_http are enabled
  • Consider adding ProxyPreserveHost On if needed
  • For complex routing, combine with RewriteRule directives

After making changes, always:

apachectl configtest
systemctl reload apache2

Then verify both URL forms work as expected. Monitor your access logs for any 404 errors.

If your proxied application generates relative links, you might need additional rewrites:

ProxyPass /test/ http://othersite.com/test/
ProxyPassReverse /test/ http://othersite.com/test/
RewriteEngine On
RewriteRule ^/test$ /test/ [R=301,L]

This ensures consistent URL handling throughout the application.


When configuring Apache reverse proxy, many developers encounter inconsistent behavior between URIs with and without trailing slashes. The core issue stems from how Apache's mod_proxy interprets URI patterns differently based on slash termination.

Your current configuration:

ProxyPass /test http://othersite.com/test
ProxyPassReverse /test http://othersite.com/test

This works for example.com/test but fails for example.com/test/ because Apache treats these as distinct paths. The module doesn't automatically normalize these variations.

Here's the proper way to handle both cases:

<Location /test>
    ProxyPass http://othersite.com/test
    ProxyPassReverse http://othersite.com/test
    DirectorySlash Off
</Location>

<Location /test/>
    ProxyPass http://othersite.com/test/
    ProxyPassReverse http://othersite.com/test/
</Location>

The DirectorySlash Off directive prevents Apache from automatically adding trailing slashes, while the dual Location blocks explicitly handle both URI formats.

If you prefer to standardize on one format:

RewriteEngine On
RewriteRule ^/test/$ /test [R=301,L]

ProxyPass /test http://othersite.com/test
ProxyPassReverse /test http://othersite.com/test
  • Test with curl -v to verify headers and redirects
  • Clear Apache cache after configuration changes
  • Ensure proper DNS resolution for backend servers

Check these logs when issues persist:

tail -f /var/log/apache2/error.log
tail -f /var/log/apache2/access.log