Removing the Query String from $_SERVER[‘REQUEST_URI’] in PHP
When trying to get the current request URI in a PHP file you would normally use:
When recently using this however I found that it could also contain the current query string. After digging a bit deeper it turns that the addition of the query string can differ based on server settings. Sometimes $_SERVER[‘REQUEST_URI’] will contain the query string, and other times it won’t.
As a result, I set out looking for a way to always guarantee that the query string wouldn’t be included, regardless of the server.
After a bit of research I found that the following would do exactly what I was after:
echo parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
Now, regardless of whether there was a query string present or not, the output would be the request URI by itself.