【发布时间】:2018-01-26 04:29:43
【问题描述】:
有一些主题可以将 url 从 http://example.com/page.php?id=3 重写为 http://example.com/username 或从 http://example.com/users/username 重写为 http://example.com/username。
他们使用 .htaccess 重写规则来做到这一点,但问题是如果你输入 example.com/username 它会重定向到http://example.com/page.php?id=3
但在 facebook 等上,url 仍然与 facebook.com/username 相同。它不会重定向到新的 url。
他们是怎么做到的?
我的想法是创建像username.html这样的动态网页作为内存效率低下
我的 .htaccess 文件:
RewriteEngine on
RewriteRule ^([^/]+)$ userinfo.php?username=$1 [L,QSA]
还有我的 userinfo.php
<?php
require_once ("config.php");
if(isset($_GET["username"]))
{
$username = $_GET["username"];
$stmt = $connection->prepare("SELECT * FROM userxmeta WHERE username = :username ");
$stmt->bindParam(":username",$username,PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount()>0)
{
$row = $stmt->fetch(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($row);
}
}
?>
【问题讨论】:
-
您能否更新您的问题以包含您当前的代码?
-
您可以重定向或仅重写。重写将请求作为 GET 发送到服务器,用户的路径仍然可读。
-
您应该尝试自己编写代码。在doing more research 之后,如果您有问题发布您尝试过的方法,并清楚地解释什么不起作用,并提供a Minimal, Complete, and Verifiable example。阅读How to Ask 一个好问题。请务必take the tour 并阅读this。
-
@Jasper 代码已添加。