Combine RedirectMatch and ProxyPass in Apache VirtualHost

apache-2.4virtualhost

I have enabled HTTPS for a site and all requests to HTTP should be redirected to HTTPS now, except for the content of one directory, e.g. /downloads.

I changed my VirtualHost configuration for HTTP as follows and added the RedirectMatch setting:

<VirtualHost *:80>
        ServerName              example.com
        RedirectMatch           302 ^/(?!download/).*$ https://example.com$1
        ProxyPreserveHost       On
        ProxyRequests           Off
        ProxyPass               /       http://10.0.0.11:3000/
        ProxyPassReverse        /       http://10.0.0.11:3000/
</VirtualHost>

# configuration for HTTPS down here, working fine

My expectation: Requests such as http://example.com/faq would be redirected to the HTTPS version, and all requests such as http://example.com/download/file.zip would still be HTTP.

But obviously, the Proxy* instructions take higher precedence and no redirect at all takes place.

How would I have to structure my configuration correctly, so that I can use the RedirectMatch, and in case it does not match uses the Proxy* setting?

Best Answer

Okay, didn't see the forest for the trees. Solution was as easy as:

<VirtualHost *:80>
        ServerName              example.com
        RedirectMatch           302 ^/(?!download/).*$ https://example.com$1
        ProxyPreserveHost       On
        ProxyRequests           Off
        ProxyPass               /download/       http://10.0.0.11:3000/download/
        ProxyPassReverse        /download/       http://10.0.0.11:3000/download/
</VirtualHost>

So, the Proxy directives only kick in for the routes which are excluded by RedirectMatch.

Related Topic