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.ex开发者_运维知识库ample/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!
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
You can access it from php using
$_SERVER['QUERY_STRING']
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
The first example is a query string; the second and third examples are not. Learn more about URLs from What Every Developer Should Know About URLs. To use the id from the query string, you need to parse it using an appropriate library.
What are the reasons that developers choose to implement their web apps' URLs using the first example instead of the second and third examples?
Aesthetics. Read about cool URIs to get some good advice on how to design URIs.
how would one implement the first example in PHP
You're looking for $_SERVER['PATH_INFO']
but beware: some hosting providers put bogus stuff in there.
In those situations, you'll need to use mod_rewrite or similar..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+) /index.php?id=$1 [L,QSA]
That way, you can get the ID out of $_GET['id']
This actually has to do with modRewrite rules or apache configurations. Some frameworks like Code Ignitor use these routes to direct you to a page that really doesn't technically exist at that physical location . It is parsed and the framework determines which view is approrpriate to display. Some rules allow the web server to pass anything after the / as a parameter to determine if a valid route is available from the framework. Some systems do not do this automatically and so a question mark is placed to force it to be passed to the framework as the route
If you would like to get the 123 from the first instance you need to pull the request query_string
If you're using Apache (and you can do the same on other servers, but I don't know the details), something along these lines will do an interesting trick:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1
</IfModule>
Rule: If the requested page is not a file !-f
or a directory !-d
then, instead of giving a page not found
error, load index.php
and pass the requested path onto that script in the form of $_SERVER['PATH_INFO']
(it can also be extracted from $_SERVER['REQUEST_URI']
.
Or you could tell Apache to rewrite to index.php?$1
, in which case the path could be extracted from $_SERVER['QUERY_STRING']
. (In that case, you should use the QSA
argument to append the current query string (if any) without messing anything up.
精彩评论