有几个主题可以将网址从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相同。它没有重定向到新网址。
他们是如何做到的?
我的想法是创建像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);
}
}
?>
答案 0 :(得分:0)
我找到了答案 .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]