2

let's say I have abc.com and def.com. On def.com, I have a def.com/test.html that displays "hello".

I want to make it so that if I do a GET abc.com/something/, it returns a 200 with the contents of def.com/test.html directly, and not a 301 def.com/test.html. I don't want users to -know- communication between 2 servers happened.

Now, I could setup a abc.com/.htaccess to url rewrite to abc.com/test.php which would take care of echoing the contents of the other website with CURL like this:

<?
$url = "def.com/test.html"; //Would be received from the .htaccess's URL rewrite, say $_GET['wantedURL']
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

But, is there a way to tell the .htaccess to do the CURL by itself (or return the output of a shell script that would do so), something like the following ?

RewriteEngine on
RewriteRule ^/something/$ <curl def.com/test.html> [L]
#NOT THIS: RewriteRule ^/something/$ def.com/test.html [R=301,L]

Of course, I would be doing that for several dynamic URLs of a given pattern, so I've just hardcoded paths to explain what I'm looking for

thanks !

--- EDIT ---

I found Can ProxyPass and ProxyPassReverse work in htaccess? which seems to require to put it in vhosts configuration, and that's something I want to avoid. I would prefer if it could be done via .htaccess. Trying the above in .htaccess yields what they indeed explain to be impossible: ProxyPassReverse not allowed here

--- EDIT 2 ---

Following @MrWhite's recommendations, I now get No protocol handler was valid for the URL /something/. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule

But I think everything is loaded. When I do httpd -M | grep "proxy" I get:

proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)

BTW this is my vhost:

<VirtualHost *:80>
    ServerName abc.com
    ProxyPass "/" "https://def.com/"
    ProxyPassReverse "/" "https://def.com/"
</VirtualHost>
MB101874
  • 33
  • 3

1 Answers1

0

In .htaccess you can use the P (proxy) flag on the mod_rewrite RewriteRule directive to send the request via mod_proxy.

For example in the .htaccess file at abc.com:

RewriteEngine on
RewriteRule ^something/$ https://def.com/test.html [P]

(Note, that when used in .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash.)

However, this naturally requires that mod_proxy and related modules have already been loaded/enabled in the server config.

And if you don't set ProxyPassReverse in the server config (as you state, this is not allowed in .htaccess) then it would be trivial for the target domain (ie. def.com) to issue a redirect to itself and effectively break out of the reverse proxy. Although, this is not necessarily an issue if the target domain is in your control.

MrWhite
  • 43,179
  • 8
  • 60
  • 84