PHP 中的 Facebook 自定义个人资料页面
Custom Profile Page like Facebook in PHP
有一些主题需要从 http://example.com/page.php?id=3 to http://example.com/username or from http://example.com/users/username to http://example.com/username 重写 url。
他们使用 .htaccess 重写规则来做到这一点,但问题是如果您键入示例。com/username 它会重定向到 http://example.com/page.php?id=3
但在 facebook 等上 url 仍然与 facebook 相同。com/username。它不会重定向到新的 url.
他们是怎么做到的?
我的想法是创建像 username.html 这样的动态网页,因为内存效率低下
我的 .htaccess 文件:
RewriteEngine on
RewriteRule ^([^/]+)$ userinfo.php?username= [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);
}
}
?>
我从中找到了答案
.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=&r [L,QSA]
有一些主题需要从 http://example.com/page.php?id=3 to http://example.com/username or from http://example.com/users/username to http://example.com/username 重写 url。
他们使用 .htaccess 重写规则来做到这一点,但问题是如果您键入示例。com/username 它会重定向到 http://example.com/page.php?id=3
但在 facebook 等上 url 仍然与 facebook 相同。com/username。它不会重定向到新的 url.
他们是怎么做到的?
我的想法是创建像 username.html 这样的动态网页,因为内存效率低下
我的 .htaccess 文件:
RewriteEngine on
RewriteRule ^([^/]+)$ userinfo.php?username= [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);
}
}
?>
我从中找到了答案 .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=&r [L,QSA]