1

The basic idea is simple:

My server hosts two domains - alice.com and bob.com. For the most part they are different, unrelated websites, but there is one URL that used to be at alice.com that now needs to go under bob.com. In other words, when a request comes in to http://alice.com/webhook.php, I want the request to land at http://bob.com/webhook.php.

So far so simple, until you take the wrinkles into account:

  • This is a webhook that an external 3rd party server calls. I cannot get the 3rd party to change the URL and I don't know if they are capable of following HTTP redirects, so I assume the worst. Thus, I don't want a HTTP redirect - I simply want Apache to handle this internally. (For the curious - this is PayPal's IPN callback).
  • I don't own or fully control the server - it's managed by the hosting company. In particular, I don't have root access and cannot edit Apache's full configuration. I can only use .htaccess files to achieve this (or, in the worst case, I could also just make a bit of PHP code that proxies the request...)
    • However I can ask the hosting company to add Apache modules and they will do it, if it's a reasonable request.
  • The way it's set up, each website runs under a different user account (via PHP-FPM), so I can't just use PHP's include().
  • The domains alice.com and bob.com are run through Cloudflare. My server responds to these domains, but the DNS records actually point them to Cloudflare. So while I can set up mod_proxy to simply issue a background request to the other domain, this will make an unnecessary loop through Cloudflare and introduces an extra point of failure. It's an option, but I'd prefer to avoid this. In fact, proxying of any kind seems like an unnecessary step, since both websites run under the same Apache. I just need to rewrite the Host...

Can this be achieved somehow?

Vilx-
  • 104,512
  • 87
  • 279
  • 422
  • People being persistent enough appear to have had success in some cases with getting their IPN webhook URL changed manually by PayPal support, https://stackoverflow.com/a/67033963/1427878 - maybe worth a try? – CBroe Aug 31 '23 at 06:03
  • @CBroe Huh. Nice. That means I can set it up both ways and then make the switch – Vilx- Aug 31 '23 at 09:01

1 Answers1

0

each website runs under a different user account (via PHP-FPM), so I can't just use PHP's include().

Although you could presumably use PHP's fopen wrappers and make an HTTP request (ie. include('http://bob.com/webhook.php')) - but this obviously triggers an external HTTP request, which you are trying to avoid. Although this is likely to be the easiest solution in this scenario I would think.

proxying of any kind seems like an unnecessary step, since both websites run under the same Apache.

But (and it's a big BUT) you don't have access to the server config. If you did then it should be relatively trivial to configure an Alias (or AliasMatch) in the virtual host container to "rewrite" requests to a different area of the filesystem. Or you could use mod_rewrite. No "reverse proxy" is required.

However, .htaccess (in the userland filesystem) imposes additional security restrictions. You cannot configure an Alias here. And you cannot internally rewrite to arbitrary areas outside of the document root.

From your description it sounds like a "reverse proxy" would be the only other solution (and accept this would trigger an external HTTP request). However, not only do you need to install mod_proxy, mod_proxy_http (and possibly other related modules), you "may" also need to configure ProxyPassReverse (depending on whether the target domain issues any (canonical) redirects - the redirect would otherwise "break" the proxy) - which can only be configured in the virtual host / server config. If this can be avoided then it would just be a matter of using mod_rewrite in .htaccess to call mod_proxy on this one URL. For example:

# alice.com/.htaccess

# Proxy requests to alternative domain
RewriteRule ^webhook\.php$ http://bob.com/$0 [P]

(Where $0 is a backreference to the entire match by the RewriteRule pattern. Just saves repeating webhook.php.)

However, this is making assumptions about any external assets that the target page (webhook.php) might be using. Obviously, any relative links are now relative to alice.com, not bob.com (so might also need to be proxied). EDIT: "this is PayPal's IPN callback" - Ah, so probably OK in this respect.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • The unpleasant wrinkle with proxying is the roundtrip through Cloudflare. Any idea how to circumvent that? Like, I could maybe proxy to `http://localhost/webhook.php` and inject the correct `Host` header? – Vilx- Aug 30 '23 at 15:48