温馨提示×

怎么用PHP和htaccess获取URLs

小亿
82
2024-04-29 16:55:48
栏目: 编程语言

要获取URL参数,你可以使用PHP中的$_GET$_REQUEST全局变量。例如,如果你的URL是http://example.com/index.php?page=about,你可以使用以下代码来获取page参数的值:

$page = $_GET['page'];
echo $page; // 输出为 "about"

如果你想重写URL并隐藏参数,你可以使用.htaccess文件来实现。例如,你可以将http://example.com/index.php?page=about转换为http://example.com/about。在.htaccess文件中添加以下代码:

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

这将把http://example.com/about重写为http://example.com/index.php?page=about。然后你可以使用上面的PHP代码来获取参数的值。

0