Is there a simple way to get the requested file or directory without the GET arguments? For example, if the URL is http://example.com/directory/file.php?paramater=value
I would like to return just http://example.com/directory/file.php
. I was surprised that there is not a simple index in $_SERVER[]
. Did I miss one?
-
Instead of using GET use POST? Or is that not possible. – Pluckerpluck Feb 29 '12 at 18:32
-
No, this is part of a larger web app. There is no way to be sure if GET or POST will be used. – Nathan Feb 29 '12 at 18:37
12 Answers
Edit: @T.Todua provided a newer answer to this question using parse_url.
(please upvote that answer so it can be more visible).
Edit2: Someone has been spamming and editing about extracting scheme, so I've added that at the bottom.
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 ?
$url=strtok($_SERVER["REQUEST_URI"],'?');
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.
An aside about Scheme (http/https) and $_SERVER
vars
While OP did not ask about it, I suppose it is worth mentioning: parse_url should be used to extract any specific component from the url, please see the documentation for that function:
parse_url($actual_link, PHP_URL_SCHEME);
Of note here, is that getting the full URL from a request is not a trivial task, and has many security implications. $_SERVER
variables are your friend here, but they're a fickle friend, as apache/nginx configs, php environments, and even clients, can omit or alter these variables. All of this is well out of scope for this question, but it has been thoroughly discussed:
https://stackoverflow.com/a/6768831/1589379
It is important to note that these $_SERVER
variables are populated at runtime, by whichever engine is doing the execution (/var/run/php/
or /etc/php/[version]/fpm/
). These variables are passed from the OS, to the webserver (apache/nginx) to the php engine, and are modified and amended at each step. The only such variables that can be relied on are REQUEST_URI
(because it's required by php), and those listed in RFC 3875 (see: PHP: $_SERVER ) because they are required of webservers.
please note: spaming links to your answers across other questions is not in good taste.

- 1
- 1

- 5,505
- 1
- 29
- 37
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:
echo $_SERVER['REQUEST_SCHEME'] .'://'. $_SERVER['HTTP_HOST'] . explode('?', $_SERVER['REQUEST_URI'], 2)[0];
...returns, for example:
http://example.com/directory/file.php
php.com Documentation:
-
Awesome. I knew it had to be something simple. I removed the last argument on explode and it worked like a charm. Thanks! – Nathan Feb 29 '12 at 19:01
-
@BrNathanH, I edited the limit so that it is now 2. It won't matter for you, but in the event you want the query string portion (after `?`), and someone has stuck a second `?` in there, then limiting the `explode()` at 2 elements is the thing to do. – Brad Mar 01 '12 at 04:34
-
-
2While this may be a good answer, it is always best to explain the reasons behind your answer http://stackoverflow.com/help/how-to-answer – Daniel Casserly Apr 16 '15 at 14:01
-
1
-
4I reckon this is the best answer, needs no explanation in my opinion, if we need explanation we'll go to the docs – pythonian29033 Apr 21 '17 at 12:56
-
2@pythonian29033, but as you mentioned, "go to the docs" - then there should be at least link to the docs. – Jul 26 '17 at 19:38
-
2
-
1
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.

- 20,365
- 9
- 72
- 105

- 2,284
- 2
- 21
- 41
Here is a solution that takes into account different ports and https:
$pageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
if ($_SERVER['SERVER_PORT'] != '80')
$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF'];
else
$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
Or a more basic solution that does not take other ports into account:
$pageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

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

- 2,728
- 20
- 18
-
-
@j4k3 it may look like copy paste, but I am actually using the same code in my project. – mahfuz Jan 29 '19 at 13:15
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: "stackoverflow.com"), 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.

- 31
- 6
-
1Could you explain any advantages over `parse_url` and/or `strtok`? – Tony Chiboucas Jul 23 '18 at 15:52
-
I expect what you're focusing on is the preg_match... With strtok() you could indeed get the same behaviour as you do with preg_match(), I don't know that you'd see any performance differences. I just prefer preg_match and consider it more common and better documented, so other developers will have an easier time with it. You could most likely use parse_url and take everything you need from it. That might be a more light-weight approach, but I haven't found reason to test it. But people apparently don't know it, and so getting things from the array it creates would be less accessible, imo. – RasmusLD Jul 24 '18 at 06:35
-
1I'm a regex fanatic, so I understand the desire to use it as magic-pill for all your parsing needs. However, `parse_url` is [clearly documented](http://php.net/manual/en/function.parse-url.php), and much more easily understood in code by those unfamiliar with regex. `explode` will perform better, `strtok` will perform the best, with regex the slowest options per [benchmarks](http://maettig.com/code/php/php-performance-benchmarks.php) and [php docs](http://us3.php.net/manual/en/function.preg-split.php). Thus, I'm curious as to the advantages of this over the other answers here? – Tony Chiboucas Oct 31 '18 at 18:42
-
I didn't mean to suggest your answer is wrong, it is still a good answer. – Tony Chiboucas Sep 27 '19 at 21:43
-
1You're of course right, if people aren't familiar with regex, then it's a bother to run into it. Thing is, at the company where I work, every developer knows regex (only one person isn't "fluent" in it), but from talking to each other, I've found that people here, understand regex better than they do parse_url and strtok. And since it's a simple operation that isn't iterated over, performance isn't as valuable to us, as is readability. In summation, the approach is best for us, because of the criteria we measure it by. But saying it's THE best approach in general, would be bad science. – RasmusLD Sep 30 '19 at 11:57
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"]);
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:
http://example.com/directory/file.php?paramater=value
...and you would like to return just:
http://example.com/directory/file.php
then use:
echo $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];

- 20,365
- 9
- 72
- 105
-
why are you spamming this all over comments and questions? Leaving aside that OP did ***not*** ask about the scheme, `$_SERVER['PHP_SELF']` is not reliable, and should not be used like that as it will often be empty in CGI/FCGI implementations. – Tony Chiboucas Aug 15 '19 at 17:05
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:
<?php
session_start();
if (isset($_REQUEST['param']) && !isset($_SESSION['param'])) {
// Store all parameters received
$_SESSION['param'] = $_REQUEST['param'];
// Redirect without URI parameters
header('Location: /file.php');
exit;
}
?>
<html>
<body>
<?php
echo $_SESSION['param'];
?>
</body>
</html>
EDIT
use $_SERVER['PHP_SELF']
to get the current file name or $_SERVER['REQUEST_URI']
to get the requested URI

- 17,612
- 7
- 60
- 88
-
Thanks, but I want to know what page they requested without the GET parameters. – Nathan Feb 29 '12 at 18:39
-
-
@ashleedawg then downvote it (or just read the `EDIT` for the right answer), not sure why I need to be alerted to something I did wrong ~7 years ago - are you auditing the rest of the answers, too? – JKirchartz May 13 '19 at 18:17
Why so complicated? =)
$baseurl = 'http://mysite.com';
$url_without_get = $baseurl.$_SERVER['PHP_SELF'];
this should really do it man ;)

- 1,258
- 1
- 11
- 20
-
4Thanks, I use RewriteEngine to filter everything through index.php (an MVC app), so this would not work. – Nathan Feb 29 '12 at 18:46
-
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 :)
-
Please elaborate on how this solves the problem stated in the question. – aravind Mar 21 '14 at 08:05
-
In my case, all POST parameters are included in the link generated by $_SESSION['PHP_SELF'] by default, but I noticed that if I add a '?' at the end, it won't add any parameters. – T Lannister Mar 21 '14 at 09:05