Take the 2-minute tour ×

How can I access a variable GET variable from PHP if the URL is like this: http://example.com/somehash

I obviously know how to do it when there is something like this: http://example.com/f.php?h=somehash

Here I would just take $_GET["h"], but i don't know how to do it for the first case...

share|improve this question
There is no GET variable in the "first case"... –  Linus Kleen Dec 5 '12 at 19:12
Search for rewrite rules. Thousands of examples here. –  Brad Dec 5 '12 at 19:13
add comment

5 Answers

up vote 3 down vote accepted

The answer is that you can't. At least not without some additional web server configuration. Typically to achieve "clean URL's" like this in PHP, there is a URL rewrite that is performed in the webserver (mod_rewrite for example on Apache). What this does is silently redirect requests for


to be handled by a script at


This allows PHP to populate $_GET as it normally would, while still showing a clean URL in the browser.

You can search StackOverflow or Google for any number of examples of how to do URL rewrite for clean URL's.

share|improve this answer
add comment

If the server has rewrite rules that change the URL to the prettyfied one, it can still be accessed as normal. ($_GET) This is because the script interprets the URL the same way, but the mod-rewrite rules change the displayed URL to make it more pretty/easier to link back.

Check out my answer at: Need some answer about example.php/example for more information.

share|improve this answer
add comment

There are multiple pieces in play here: when a user requests http://example.com/some-page, the web server they connected to receives an HTTP request asking for the example.com domain and the path some-page.

Your web server picks that up, matches the domain to the right virtual host, and then hands off the request to that domain. Because there is likely no file named some-page in the domain's document root, the web server will send back a 404 Not Found error message.

You want to tell your web server to redirect all requests that don't match a known file or directory to go to a default script, like index.php. If you're using Apache, you can use HTTP rewrite rules to accomplish this. When Apache sends the request, it will usually be completely unmodified. You will be able to use $_SERER['REQUEST_URI'] to pick up the path you want.

share|improve this answer
add comment

Though you may want to use rewrite rules to do it properly and all, You can still use basic PHP to find URI segments, assuming you already have the rewrite rules in place to allow the specified URL to resolve in the first place...

function uriSegments()
    return explode("/", $_SERVER['REQUEST_URI']);

$segments = uriSegments();

echo $segments[1];

That should get you "somehash"

share|improve this answer
add comment

From PHP, you can consult the $_SERVER['REQUEST_URI"] value, and apply some text transformation (substrings, regexp) if needed, but in your case using the value directly should work.

share|improve this answer
add comment

Your Answer


