Umami
PHP Proxy
To serve the umami tracking script and api over the same host I use the following simple php script.
umami-proxy.php
<?php
// Define the target URL
$targetUrl = "<umami-host>";
// Get the client request URL
$clientUrl = $_SERVER['REQUEST_URI'];
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $targetUrl . $clientUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$request_headers = [
'Content-Type:application/json',
'Accept:application/json'
];
if ($_SERVER['REQUEST_METHOD'] === "POST") {
$body = file_get_contents('php://input');
$data = json_decode($body, True);
if ($_SERVER['REQUEST_URI'] === "/api/send") {
$data["payload"]["ip"] = $_SERVER['REMOTE_ADDR'];
}
array_push($request_headers, "User-Agent:{$_SERVER['HTTP_USER_AGENT']}");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
}
// Execute the cURL request
$response = curl_exec($ch);
// Get the response headers
$responseHeaders = curl_getinfo($ch);
// Close the cURL session
curl_close($ch);
// Forward the response headers to the client
foreach ($responseHeaders as $headerName => $headerValue) {
header("$headerName: $headerValue");
}
// Forward the response body to the client
echo $response;
?>
To serve only the required endpoint over the proxy add the following config to your .htaccess
.