John Davidson

Why is PHP $_SERVER[REQUEST_URI] different on local and production servers?

0 comments
Message:


I have a 404 page that sends a Slack message through a CURL request to Zapier, before redirecting to a different page.


<?php
/* Send a silent alert to Slack to record the missing page, then redirect to /apply */
$host = $_SERVER['HTTP_HOST'];
$page = $_SERVER['REQUEST_URI'];


$url = "https://hooks.zapier.com/hooks/catch/<webhook_id>";

$curl = curl_init();

$data = array("page"=> array("href"=> "https://$host$page"));

$jsonEncodedData = json_encode($data);

$opts = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $jsonEncodedData,
CURLOPT_HTTPHEADER => array('Content-Type: application/json','Content-Length: ' . strlen($jsonEncodedData))
);

curl_setopt_array($curl, $opts);

curl_exec($curl);

header("Location: https://$host/apply");

?>

I'm seeing different behavior from this code on my local set up (MAMP PRO), and the production set up (nearlyfreespeech.net).


On localhost, it works perfectly. So if I load the page: localhost:8888/not-a-page, I get a slack alert with the full url: https://localhost:8888/not-a-page.


However, on the server, the $_SERVER['REQUEST_URI'] variable appears to be blank, and I'd get a slack alert with just the root domain of the production site: https://example.com instead of the desired https://example.com/not-a-page. However, if I add a file extension to the url like https://example.com/not-a-page.php, then the full URL comes through correctly.


I'm stumped. Any ideas on what to check?


EDIT


Thanks to Justinas for the advice to check for redirects. I double checked my .htaccess file, and saw that I had set up the 404 handler like this: ErrorDocument 404 /alert404, instead of ErrorDocument 404 /alert404.php.


So there was a redirect from alert404->alert404.php happening that for some reason was setting the $_SERVER[REQUEST_URI] variable to an empty string before the alert script ran. Making that change fixed the problem.


That said, I'm still not sure why this was behaving differently on my local server and production servers.



My Answer:
The reason why $_SERVER['REQUEST_URI'] was different on your local server and production server is likely due to differences in server configurations. Local servers like MAMP PRO may have different settings or configurations compared to production servers like nearlyfreespeech.net.

In this case, the issue was related to the .htaccess file and the way the 404 handler was set up, causing a redirect that affected the $_SERVER['REQUEST_URI'] variable. Making the necessary changes in the .htaccess file resolved the issue.

It's not uncommon for there to be differences in behavior between local and production servers due to various factors such as server settings, configurations, and environment variables. It's always a good idea to thoroughly test your code on both local and production servers to ensure consistent behavior.

Rate this post

5 of 5 based on 1569 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us