Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between /123 and /?123?

I've noticed that some sites (including the old http://careers.stackoverflow.com 1.0) have query strings that look like this:

http://somewebapp.example/?123

as compared to:

http://somewebapp.example/123 or http://somewebapp.example/id/123

What are the reasons that developers choose to implement their web apps' URLs using the first example instead of the second and third examples?

And as a bonus, how would one implement the first example in PHP, given that 123 is the primary key of some row in a database table? (I just need to know how to retrieve 123 from the URL; I already know how to query the database for a primary key of 123.)

EDIT [5/28]: Oops, forgot to let everyone know that I know what the latter two URLs are, how they work and how to implement them. Thanks for the reminders though, I think I had some unrelated misconceptions that were serendipitously clarified anyway!

like image 650
BoltClock Avatar asked May 28 '10 17:05

BoltClock


3 Answers

The first is easier to implement; everything after the ? is part of the query string. The web server loads the page specified before the ?, and handles the query string separately (in PHP it's accessible through $_GET)

In the second example the developer needs to set up the web server to redirect all requests to a special page (since there is no /123 page on the server), which will then parse the URL to figure out what was requested

As to your last question, the 123 will show up in $_GET as a key, so key($_GET) would work assuming it's the only thing you're passing in the query string

like image 84
Michael Mrozek Avatar answered Oct 22 '22 14:10

Michael Mrozek


You can access it from php using

$_SERVER['QUERY_STRING']
like image 30
dalton Avatar answered Oct 22 '22 15:10

dalton


The first URL is using query parameters to send the data. The later is a form of REST URL which actually is pointing to a resource with ID 123

like image 30
Teja Kantamneni Avatar answered Oct 22 '22 16:10

Teja Kantamneni