设置干净的 URL,如 CodeIgniter [英] Setup clean URLs like CodeIgniter
问题描述
我喜欢 CodeIgniter 清理站点 URL 的方式,但是,我有一个站点过于复杂,无法使用 CI 框架重新开始(至少现在是这样),而且我不需要 CI 提供的深度,我只需要有一层深度.
I like the way CodeIgniter cleans URLs for sites, however, I have a site that is too involved to start over using the CI framework (at least for now), and I don't need the depth CI provides, I only have one level deep.
是否有一种简单的方法可以使用直接的 PHP 轻松地做到这一点?
Is there an easy way to do this easily using straight PHP?
index.php?id=2454
index.php/2454/
注意:我需要一个直接的 PHP 解决方案,因为服务器是 Windows 并且 .htaccess 没有设置为工作.
NOTE: I need a straight PHP solution because the server is Windows and .htaccess is not setup to work.
推荐答案
检查 $_SERVER['PATH_INFO']
- 它返回脚本文件名后但查询前的任何内容.
Check out $_SERVER['PATH_INFO']
- it returns anything trailing the script filename but preceding the query.
例如,在网址中:
http://www.domain.com/index.php/var1/var2
$_SERVER['PATH_INFO']
将包含 /var1/var2
然后您可以在 __construct()
或 init()
等中编写一个函数来解析路径(例如 explode("/",$_SERVER['PATH_INFO'])
) 并将结果数组用作变量.
You could then write a function in your __construct()
or init()
, etc, to parse the path (e.g. explode("/", $_SERVER['PATH_INFO'])
) and use the resulting array as variables.
这篇关于设置干净的 URL,如 CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!