对 $_GET 使用 SO 的 URL 格式

发布于 2024-10-07 16:10:59 字数 350 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

伪心 2024-10-14 16:10:59

那么,使用.htaccess来分割URL,URL不会改变,而是指向带有各种$_GET变量的index.php,这可能是增加以覆盖更多 URL 部分。

# turn rewriting on
RewriteEngine on

# get variables in this order, [object], [object,action], [object,action,selection]
RewriteRule ^([^/\.]+)/?$ /index.php?object=$1 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2&selection=$3 [L,NC,QSA]

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.

# turn rewriting on
RewriteEngine on

# get variables in this order, [object], [object,action], [object,action,selection]
RewriteRule ^([^/\.]+)/?$ /index.php?object=$1 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2&selection=$3 [L,NC,QSA]
星軌x 2024-10-14 16:10:59

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.

等你爱我 2024-10-14 16:10:59

它实际上是构建 URI 的RESTful 方式。不仅如此,应用了这种模式。我建议不要通过查看这个问题来重新发明轮子。

此外,您可以切换到 RESTful 框架,例如 CakePHPCodeIgniter,默认配置为使用 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.

陈独秀 2024-10-14 16:10:59

$_GET 不包含 URL 中的路径部分,仅包含最终跟随在 ? 后面的参数。您可以使用

$parts = explode('/', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME));
var_dump($parts);

但是似乎您应该阅读有关 URL 重写的内容,例如使用 mod_rewrite。 “我不希望 URL 被重写 - 我希望它保持干净和友好”......重写发生在服务器上。用户永远不会看到“丑陋”的结果。

$_GET does not contain the path compontents from the URL, only the parameters that eventually follow the ?. You could use

$parts = explode('/', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME));
var_dump($parts);

However 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.

耳钉梦 2024-10-14 16:10:59

如果您不想使用 mod 重写,最好的解决方案是对 $_SERVER['REQUEST_URI'] 变量使用正则表达式。

即:

preg_match('|/(.*)/(.*)|', $_SERVER['REQUEST_URI'], $match);
$_GET['param1'] = $match[1];
$_GET['param2'] = $match[2];

如果你想设置一个捕获所有 php 脚本。 IE 如果脚本请求不存在,则使用默认脚本,使用 mod-rewrite 将所有内容重定向到一个脚本,即 zend 框架(以及大多数 PHP MVC 框架)使用此脚本:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

我认为这可能有点麻烦。

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:

preg_match('|/(.*)/(.*)|', $_SERVER['REQUEST_URI'], $match);
$_GET['param1'] = $match[1];
$_GET['param2'] = $match[2];

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:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

I think that could be a bit cumbersome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文