【问题标题】:Custom Profile Page like Facebook in PHP自定义个人资料页面,如 PHP 中的 Facebook
【发布时间】: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 代码已添加。

标签: php profile


【解决方案1】:

我找到了答案 .htaccess RewriteRule to path without changing URL

 # Tell PHP that the mod_rewrite module is ENABLED.

SetEnv HTTP_MOD_REWRITE On  
RewriteEngine on
RewriteBase /

 #For internal pages(rewrite "/page.php?name=about us" to "AboutUs") 

RewriteCond %{QUERY_STRING} ^name=([^&\s]+)$  
RewriteRule ^(?:page\.php|)$ /%1? [R=301,L]  

RewriteCond %{QUERY_STRING} ^name=([^&\s]+)&name=([^&\s]+)$  
RewriteRule ^(?:page\.php|)$ /%1/%2? [R=301,L]  

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^([^\s\/]+)/?$ page.php?name=$1&r [L,QSA] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    相关资源
    最近更新 更多