Debian – apache2 mod_proxy in conjuction with mod_rewrite not working as expected

amazon-web-servicesapache-2.2debianmod-proxymod-rewrite

I am trying to use mod_rewrite in conjunction with mod_proxy to deliver contents from a different internal webserver (http://webserver.internal.com/foo) through a (fake) subdirectory "foo" (http://example.com/foo).

Using mod_proxy with ProxyPass directive I was able to do this already with this configuration in /etc/apache2/mods-enabled/proxy.conf

ProxyPass /foo http://webserver.internal.com/foo
ProxyPassReverse /foo http://webserver.internal.com/foo

Now I have the additional requirement to enforce rewriting all requests to dynamic resources from http://example.com to https:// example.com, which again standalone works as well, but not together with the ProxyPass rules stated above. It seems the proxy configuration has always precedence over the rewrite rules.

As as far as my research has shown me utilizing mod_proxy from within my rewrite rules should be as easy as adding

RewriteCond %{REQUEST_URI} ^/foo
RewriteRule ^/foo(.*) http://webserver.internal.com/foo$1 [P]

to my rewrite rules and the rewritten URI will be automatically passed through mod_proxy. I turned on RewriteLog with a LogLevel of 9, but I am not able to see the matching of ^/foo and execution of the following Proxy-RewriteRule afterwards. There is nothing like go-ahead proxy, or any mention of proxying, not even an error entry within the RewriteLog.

I have the following proxy modules activated in my apache2 configuration

proxy 
proxy_connect 
proxy_http

I tried to test my RewriteCondition with another example – this time matching ^/foo and redirecting to www.google.com

RewriteCond %{REQUEST_URI} ^/foo
RewriteRule ^(.*)$ http://www.google.com/ [R=302,L]

which is working as intended, with the following results in the RewriteLog:

RewriteCond: input='/foo' pattern='^/foo' => matched
rewrite 'foo' -> 'http://www.google.com/'
explicitly forcing redirect with http://www.google.com/
escaping http://www.google.com/ for redirect
redirect to http://www.google.com/ [REDIRECT/302]

Following below is the rewrite configuraiton I am using within a directory section of my vhost.

    <Directory /my/app/webroot >
            RewriteEngine On
            RewriteBase /

            #redirect all http requests to https - WORKING
            # incoming request was forwarded as http from ELB
            RewriteCond %{HTTP:X-Forwarded-Proto} =http
            # avoid https rewriting for static files like images, javascripts, files and templates
            # for folders /img, /js, /css, /files and /templates under webroot
            RewriteCond %{REQUEST_URI} !^/(img|js|css|files|templates)
            RewriteRule (.*) https:// example.com%{REQUEST_URI}  [R=301,L]

            # requests to /foo *should* be handled by mod_proxy - NOT WORKING
            RewriteCond %{REQUEST_URI} ^/foo
            RewriteRule ^/foo(.*) http://webserver.internal.com/foo$1 [P]

            #pretty URLS for web-application - WORKING
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    </Directory>

The machine in question is a basic debian server installation, with apache2 and php and is hosted in the amazon cloud behind a Elastic Load Balancer.

If you read down this far – first of all – thank you for patience!

It would be great if somebody could help with resolving this problem. Maybe it is just a little think I've overseen.

Many thanks in advance!

Best Answer

Wrong:

RewriteRule ^/foo(.*) http://webserver.internal.com/foo$1 [P]

Сorrectly:

RewriteRule ^foo(.*)$ http://webserver.internal.com/foo$1 [P,L]

Will be a problem, write to email.

Related Topic