Skip to content

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);

if ($_SERVER['REQUEST_METHOD'] === "POST") {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept:application/json'));
}


// 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.

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^script.js$ umami-proxy.php
RewriteRule ^api/send$ umami-proxy.php
</IfModule>