Joe Fletcher
Joe Fletcher

Reputation: 2181

Can ProxyPass and ProxyPassReverse work in htaccess?

I've never set up a proxy before. I'm using shared hosting, so to set Apache directives, I need to use .htaccess. Can I use .htaccess to do something like below? Any limitations?

ProxyRequests Off
ProxyPass /img/ http://internal.example.com/img/
ProxyPass /app/ http://internal.example.com/app/

ProxyPassReverse / http://internal.example.com/

Upvotes: 51

Views: 89558

Answers (2)

Mike J-P.
Mike J-P.

Reputation: 141

You can't use ProxyPassReverse, but you can mimic it if you have the ability to rewrite the HTML as it comes back from the origin server.

See my writeup here.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143856

You cannot use a ProxyPass in an htaccess file. The documentation says it is only applicable in the context:

Context: server config, virtual host, directory

which excludes htaccess (you can't have a <Directory> block in htaccess). However, you can use a ProxyPassReverse to internally rewrite the Location field of proxied requests that cause a redirect. You'll just need to use mod_rewrite's P flag to proxy instead of ProxyPass. So something like:

RewriteEngine On
RewriteRule ^/?img/(.*)$ http://internal.example.com/img/$1 [L,P]
RewriteRule ^/?app/(.*)$ http://internal.example.com/app/$1 [L,P]

ProxyPassReverse / http://internal.example.com/

Just to be clear, you cannot use ProxyPass or ProxyPassReverse in the htaccess file, but you can use ProxyPassReverse with mod_rewrite rules that utilize the P flag.

Upvotes: 52

Related Questions