2

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

  1. http://localhost/findbyproductcode/4xxheua
  2. http://localhost/findbyproductcode?productcode=4xxheua

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.

Mattia
  • 47
  • 1
  • 9
  • _"I know that a query string for a web service, should be look like more 1 than 2"_ - why? – Halcyon Apr 27 '12 at 20:12
  • I cant really see the convenience, but it is referred by many that is more elegant and it is somehow the right way to do it...but, is this just a url rewrite rule? i think no, and i just cant figure out how this should work ... – Mattia Apr 27 '12 at 20:18
  • It is the way REST Api work, url/resource_name/id . Btw server side (rest server) and client side urls are handled by a multitude of existing software out there. for instance I'm using CodeIgniter Rest server for handling requests and Backbone.js on the client side. Both work pretty well and handle urls automatically, although you can (if you want to) pass additional paramenters though GET/POST/DELETE/PUT – kernelpanic Apr 27 '12 at 20:33
  • @FritsvanCampen because that way is easyer for search engine to index all your page. query string should be used to add some particular filter, see http://stackoverflow.com/questions/16086513/using-query-string-in-rest-web-services – Lesto Jan 31 '14 at 19:56

4 Answers4

2

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.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
1

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.

GDP
  • 8,109
  • 6
  • 45
  • 82
  • Yes im tring to write an API for a simple webservice (my client will probably be smartphones) – Mattia Apr 27 '12 at 20:23
1

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:

tosin
  • 1,159
  • 7
  • 14
0

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!

halfer
  • 19,824
  • 17
  • 99
  • 186