路由 URL 时使用 index.php?q=path/ 而不是 index.php/path/ 的优点/缺点?
我正在编写一个简单的方法来将路由映射到文件,并且我遇到了两种方法来实现它。
第一种,我猜大多数框架都会使用,是使用 $_SERVER['REQUEST_URI'] 变量来提取 index.php 之后的所有内容:
RewriteRule ^(.*)$ index.php [QSA,L]
第二种方法在 Drupal 中使用,路由只是作为查询字符串传递。
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
现在,“Drupal 方式”对我来说似乎简单多了。使用另一种方法,您必须在 $_SERVER['REQUEST_URI'] 和 $_SERVER['SCRIPT_NAME'] 上使用“explode”,然后使用 array_diff_assoc 之类的内容删除脚本名称和子目录名称(如果有)。这并不是很多工作,但如果使用 Drupal 方式您可以简单地提取 $_GET['q'] 值,为什么没有人这样做呢?如果有的话,有哪些缺点?
谢谢。
I'm writing a simple method to map routes to files and I've come across two ways to do it.
The first, and I guess used by most frameworks, is using the $_SERVER['REQUEST_URI'] variable to extract everything after index.php:
RewriteRule ^(.*)$ index.php [QSA,L]
The second way is used in Drupal, and the route is simply passed as a query string.
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
Now, the "Drupal way" seems a lot simpler to me. With the other method you'd have to use "explode" on both $_SERVER['REQUEST_URI'] and $_SERVER['SCRIPT_NAME'] and then use something like array_diff_assoc to remove the script name and subdirectory name, if there is one. It's not THAT much work, but if with the Drupal way you can simply extract the $_GET['q'] value, why nobody does it that way? What are the disadvantages, if any?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
q
参数的缺点是,如果没有 URL 重写,URL 将看起来像......而不是更干净的(IMO)...
The disadvantage of using a
q
param is, without URL rewriting the URL will look like......as opposed to the cleaner (IMO)...
重写 url 没有任何巨大的优点或缺点。不过,我会指出包括最后一个斜杠在内的所有内容都存储在
_SERVER[PATH_INFO]
中,因此不需要解析请求 URI。There is no huge advantage or disadvantage one way or the other with the url being rewritten. However, I will point out everything including and after the final slash is stored in
_SERVER[PATH_INFO]
, so parsing the request URI my not be necessary.大多数使用较短 URL 技术的原因是为了更干净的技术和由此带来的更好的 SEO。搜索引擎认为这两个 URL“相同”:
我没有很好的解释,所以这里是一些链接,其中包含一些非常好的信息:
现在有些人以不同的方式实现较短的网址,但这就是我发现他们的方式对我来说效果最好:
在 .htaccess
在 index.php (或其他一些 php 文件)
中这允许您使用 $_GET['1'] (或 $_GET[1])以及所有后续数字。
URL 看起来像这样:
然后可以通过以下方式访问参数:
希望有帮助!
The reason that the shorter URL technique is used mostly is for the cleaner technique and the better SEO that comes from it. Search engines consider these two URL's to "be the same":
I do not have a good explanation so here are some links with some really good information on it:
Now some people implement the shorter URL's differently, but this is how I have found them to work the best for me:
In .htaccess
In index.php (or some other php file)
This then allows you to use $_GET['1'] (or $_GET[1]) and all subsequent numbers as well.
URL's then look like this:
And then the parameters can be accessed via:
Hope that helps!