Request string without GET arguments in PHP

Is there a simple way to get the requested file or directory without the GET arguments? For example, if the URL is I would like to return just I was surprised that there is not a simple index in $_SERVER[]. Did I miss one? Thanks!


You can use $_SERVER['REQUEST_URI'] to get requested path. Then, you'll need to remove the parameters...

$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);

Then, add in the hostname and protocol.

echo 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];

You'll have to detect protocol as well, if you mix http: and https://. That I leave as an exercise for you. $_SERVER['REQUEST_SCHEME'] returns the protocol.

Putting it all together:

     . explode('?', $_SERVER['REQUEST_URI'], 2)[0];

...returns, for example: Documentation:
  • $_SERVER — Server and execution environment information
  • explode — Split a string by a string
  • parse_url — Parse a URL and return its components (alternative solution)

Another alternative method here.

Edit: @T.Todua provided a newer answer to this question using parse_url.

(please upvote that answer so it can be more visible).

parse_url solution

The simplest solution would be:

echo parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);

Parse_url is a built-in php function, who's sole purpose is to extract specific components from a url, including the PATH (everything before the first ?). As such, it is my new "best" solution to this problem.

strtok solution

Stackoverflow: How to remove the querystring and get only the url?

You can use strtok to get string before first occurence of ?


Performance Note: This problem can also be solved using explode.

  • Explode tends to perform better for cases splitting the sring only on a single delimiter.
  • Strtok tends to perform better for cases utilizing multiple delimiters.

This application of strtok to return everything in a string before the first instance of a character will perform better than any other method in PHP, though WILL leave the querystring in memory.


echoparse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);

Here is a solution that takes into account different ports and https:

$pageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';

if ($_SERVER['SERVER_PORT'] != '80')

Or a more basic solution that does not take other ports into account:

$pageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';

I actually think that's not the good way to parse it. It's not clean or it's a bit out of subject ...

  • Explode is heavy
  • Session is heavy
  • PHP_SELF doesn't handle URLRewriting

I'd do something like ...

if ($pos_get = strpos($app_uri, '?')) $app_uri = substr($app_uri, 0, $pos_get);
  • This detects whether there's an actual '?' (GET standard format)
  • If it's ok, that cuts our variable before the '?' which's reserved for getting datas

Considering $app_uri as the URI/URL of my website.

It's shocking how many of these upvoted/accepted answers are incomplete, so they don't answer the OP's question, after 7 years!

  • If you are on a page with URL like:

  • ...and you would like to return just:

  • then use:


Why so complicated? =)

$baseurl = '';
$url_without_get = $baseurl.$_SERVER['PHP_SELF'];

this should really do it man ;)

You can use $_GET for url params, or $_POST for post params, but the $_REQUEST contains the parameters from $_GET $_POST and $_COOKIE, if you want to hide the URI parameter from the user you can convert it to a session variable like so:


if (isset($_REQUEST['param']) && !isset($_SESSION['param'])) {

    // Store all parameters received
    $_SESSION['param'] = $_REQUEST['param'];

    // Redirect without URI parameters
    header('Location: /file.php');
  echo $_SESSION['param'];


use $_SERVER['PHP_SELF'] to get the current file name or $_SERVER['REQUEST_URI'] to get the requested URI

Not everyone will find it simple, but I believe this to be the best way to go around it:

preg_match('/^[^\?]+/', $_SERVER['REQUEST_URI'], $return);
$url = 'http' . ('on' === $_SERVER['HTTPS'] ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $return[0]

What is does is simply to go through the REQUEST_URI from the beginning of the string, then stop when it hits a "?" (which really, only should happen when you get to parameters).

Then you create the url and save it to $url: When creating the $url... What we're doing is simply writing "http" then checking if https is being used, if it is, we also write "s", then we concatenate "://", concatenate the HTTP_HOST (the server, fx: ""), and concatenate the $return, which we found before, to that (it's an array, but we only want the first index in it... There can only ever be one index, since we're checking from the beginning of the string in the regex.).

I hope someone can use this...

PS. This has been confirmed to work while using SLIM to reroute the URL.

I know this is an old post but I am having the same problem and I solved it this way

$current_request = preg_replace("/\?.*$/","",$_SERVER["REQUEST_URI"]);

Or equivalently

$current_request = preg_replace("/\?.*/D","",$_SERVER["REQUEST_URI"]);

$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
$request_uri = $uri_parts[0];
echo $request_uri;

I had the same problem when I wanted a link back to homepage. I tried this and it worked:

<a href="<?php echo $_SESSION['PHP_SELF']; ?>?">

Note the question mark at the end. I believe that tells the machine stop thinking on behalf of the coder :)

Need Your Help

Angular 2: Form containing child component

angular angular2-forms

I have component which has a form and some child components within the form. The child components are created using *ngFor and each child contains input elements. Angular2 compiler is giving errors...

Running a command in a Grunt Task

javascript templates gruntjs continuous-integration google-closure-compiler

I'm using Grunt (task-based command line build tool for JavaScript projects) in my project. I've created a custom tag and I am wondering if it is possible to run a command into it.