2

我将为此示例展示两种类型的查询字符串。我知道 Web 服务的查询字符串应该看起来比 1 多于 2

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

我的问题是:你如何管理 php 中的第一个?这不只是对第二个 URL 的重写吗?

使用#2 查询字符串,我想有一个 findbyproductcode.php 页面处理 GET 变量(?productcode=4xxheua),使用此产品代码检查一些数据库并发回一些数据。

#1 应该如何工作?我应该有一个 4xxheua.php 吗?(显然不是......)......我只是不明白这一点。

4

4 回答 4

2

您更喜欢 1 而不是 2 的唯一原因是因为 1 在语义上更直观。

有这样的 URL 是有意义的:

http://www.myblog.com/article/2012-04-29/i-did-nothing-all-day

而不是

http://www.myblog.com/index.php?page=article&id=23423

据说它也有助于您的 SEO 得分。

如果您只是在构建 Web 服务,例如:机器与机器对话 - 那么 URL 的外观并不重要。您也可以只拥有一个接受所有输入并根据输入数据在内部进行“路由”的 URL。

于 2012-04-27T20:14:53.697 回答
1

如果您正在尝试编写 API,则有很多工作要做 - 是的,涉及重写,但不必为所有参数提供一个页面 - Luracast 有一个很棒的开源 Rest API 调用 ed restler 开始和。

于 2012-04-27T20:14:55.950 回答
1

实现#1的一个例子,

1:将所有没有图像的路径,css(等)发送到root/index.php(例如Apache上的mod_rewrite)
2:在index.php,从$_SERVER['pathinfo']或其他方式获取参数。
3:解析处理。

如果您使用 Apatche 和 mod_rewrite,请查看文档:

于 2012-04-27T20:38:53.370 回答
0

是的,只是 URL 重写。我目前正在使用 Symfony 1.3,他们的 htaccess 看起来像这样:

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>

在这种情况下,只需将 index.php 设置为您的控制器,您就可以获取此虚拟主机的所有 URL 发送给它。简单的!

于 2012-04-27T20:45:11.977 回答