对 $_GET 使用 SO 的 URL 格式
StackOverflow 有一个非常整洁、干净的 URL 格式。它看起来与目录结构相同,但这里不可能每个问题都有一个目录!我的问题是:
例如,我怎样才能让 http://www.site.com/sections/tutorials/tutorial1
保持地址栏中的样子,但将其转换为 < code>$_GET 请求 PHP 来搞乱?
我可以使用 .htaccess
文件,但我不希望 URL 被重写 - 我希望它保持干净和友好。我唯一的选择是使用 PHP 的字符串分割函数来获取一些假装的 $_GET
数据吗?
谢谢,
詹姆斯
StackOverflow has a very neat, clean URL format. It looks the same as a directory structure, but there can't be a directory for each question on here! My question is this:
How can I get http://www.site.com/sections/tutorials/tutorial1
, for example, to stay like that in the address bar, but convert it to a $_GET
request for PHP to mess around with?
I could use a .htaccess
file, but I don't want the URL being rewritten - I'd like it to remain clean and friendly. Is my only option here to use PHP's string splitting functions to get some pretend $_GET
data?
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么,使用
.htaccess
来分割URL,URL不会改变,而是指向带有各种$_GET
变量的index.php,这可能是增加以覆盖更多 URL 部分。What about this, using
.htaccess
to split the URL up, the URL won't change but instead point to index.php with various$_GET
variables, this could could be increased to cover more URL sections.PHP Rest 框架可以为你做到这一点,所以我建议你参考这个 问题。大多数框架不会从
$_GET
加载数据,但会提供类似且同样方便的读取数据的方式。A PHP Rest framework could do this for you, so I refer you to this question. Most of the frameworks won't load the data from
$_GET
, but will offer a similar and equally convenient way to read it.它实际上是构建 URI 的RESTful 方式。不仅如此,应用了这种模式。我建议不要通过查看这个问题来重新发明轮子。
此外,您可以切换到 RESTful 框架,例如 CakePHP 或 CodeIgniter,默认配置为使用 RESTful 模式。
It's actually a RESTful way of building your URI's. Not only SO applies this pattern. I recommend to not re-invent the wheel by taking a look at this question.
In addition you could switch over to a RESTful framework such as CakePHP or CodeIgniter, which are configured by default to use the RESTful pattern.
$_GET
不包含 URL 中的路径部分,仅包含最终跟随在?
后面的参数。您可以使用但是似乎您应该阅读有关 URL 重写的内容,例如使用 mod_rewrite。 “我不希望 URL 被重写 - 我希望它保持干净和友好”......重写发生在服务器上。用户永远不会看到“丑陋”的结果。
$_GET
does not contain the path compontents from the URL, only the parameters that eventually follow the?
. You could useHowever it seems you should have a read on URL rewriting e.g. with mod_rewrite. "I don't want the URL being rewritten - I'd like it to remain clean and friendly" ... The rewriting happens on the server. The user never sees the "ugly" result.
如果您不想使用 mod 重写,最好的解决方案是对
$_SERVER['REQUEST_URI']
变量使用正则表达式。即:
如果你想设置一个捕获所有 php 脚本。 IE 如果脚本请求不存在,则使用默认脚本,使用 mod-rewrite 将所有内容重定向到一个脚本,即 zend 框架(以及大多数 PHP MVC 框架)使用此脚本:
我认为这可能有点麻烦。
If you don't want to use mod rewrite the best solution would be using regular expressions agains the
$_SERVER['REQUEST_URI']
variable.i.e:
If you want to setup a capture all php script. IE if the script request doesn't exist use a default script, use mod-rewrite to redirect everything to one script i.e. the zend framework (and most of the PHP MVC framework) use this:
I think that could be a bit cumbersome.