Search code examples
phphttpproxybackendwebproxy

PHP Web Proxy - Make all resources on web site pass through the proxy?


I am creating a simple PHP web proxy. The PHP script currently correctly fetches the page's HTML from the backend.

However clearly this is not enough since all the resources (img, script, link, a) all point to relative or absolute links e,g example.com/image.png which makes it load directly.

What would be an easy way to "proxify" all these resources to pass through, e.g example.com/image.png would be /proxy/resource.php?r=example.com/image.php or something.

If anyone knows a good way to do this I'd be delighted to know it.


Solution

  • Alright, I found a way to do so myself.

    I simply used the DOMDocument PHP class to parse the gotten HTML and scan for src and href, do some transformations to the URL and point it back to the proxy, then DOMDocument::saveHTML, and send to client.