I'll show two type of query string for this example. I know that a query string for a web service, should be look like more 1 than 2
My question is: how do you manage the first one in php? isnt this just an url rewrite for the second one?
with the #2 query string, I suppose there's a findbyproductcode.php page that process the GET variable(?productcode=4xxheua), check some databases with this productcode and send back some data.
How the #1 is supposed to work? should I have a 4xxheua.php? (obviously not...) ... i just dont get this.
The only reason you'd prefer 1 over 2 is because 1 is more semantically intuitive.
It makes sense to have a URL like:
http://www.myblog.com/article/2012-04-29/i-did-nothing-all-day
Rather than
http://www.myblog.com/index.php?page=article&id=23423
Supposedly it also helps your SEO score.
If you're just building a webservice, as in: machines talk to machines - then it doesn't matter what the URL looks like. You could also just have one URL that accepts all inputs and do the 'routing' internally based on the input data.
If you're trying to write an API, thre's a bunch to do - yes, there's rewriting involved, but there doesn't have to be a page for all the paramters - Luracast has a great opensource Rest API call ed restler to get started with.
One example to achieve #1,
1: Send all path without image, css(, and so on) to root/index.php
(e.g. mod_rewrite on Apache)
2: In index.php
, get parameters from $_SERVER['pathinfo']
or another way.
3: parse and process it.
If you use Apatche and mod_rewrite, check the docs:
Yep, just URL rewriting. I'm using Symfony 1.3 at the moment, and their htaccess looks like this:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
In this case, just make index.php your controller, and you can get all URLs for this vhost sent to it. Easy!