Removing the Query String from $_SERVER[‘REQUEST_URI’] in PHP

December 16th, 2014 - Posted by Steve Marks to PHP, Web Development.

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.

The Solution

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.

This entry was posted on Tuesday, December 16th, 2014 at 2:13 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first