参数作为url的一部分,而不是在问号之后


parameters as part of url instead of after question mark

我想知道我们是否能实现这样的目标:example.com/apps?id='blahblah'转换为example.com/apps/id/blahblah,即参数本身成为页面。我想为每个id创建单独的url,但要动态创建。这在php或任何其他变通方法中可能吗。

您需要使用.htaccess来实现这一点。

.htaccess基本上将url重写到您的原始位置。所以,按照你在问题中提出的要求,它会是这样的:

RewriteEngine on
RewriteRule apps/id/(.*?)$ apps?id=$1

你可能会被否决,因为"php中的mod重写"已经在教程和文章中得到了广泛的记录。试着在这里或谷歌上搜索,你会发现很多结果。

无论如何,如果您不能或不愿意使用Apache的mod_rewrite,那么您可以在纯PHP中通过解析HTTP请求和相应的处理来完成同样的操作。

例如

$request_path = $_SERVER['PATH_INFO'];
$request_path = explode("/", $request_path);
// example.com/apps.php/id/blahblah
// $request_path[0] => "example.com"
// $request_path[1] => "apps.php"
// $request_path[2] => "id"
// $request_path[3] => "blahblah"

注意,我在apps中添加了.php扩展。如果要隐藏.php扩展,则必须使用mod_rewrite

从这里开始,您可以使用$request_path来确定请求想要什么。

相关文章: