2017-08-17 115 views
-2

有几个主题要重写URL从http://example.com/page.php?id=3http://example.com/username或从http://example.com/users/usernamehttp://example.com/username自定义配置文件页面,如Facebook中的PHP

他们做到这一点与的.htaccess重写规则,但问题是,如果你键入example.com/username它重定向到http://example.com/page.php?id=3

但在Facebook上等网址还是一样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); 
    } 
} 
?> 
+1

您能否更新您的问题以包含您当前的代码? – Jasper

+0

您可以重定向或只是重写。重写将请求作为GET发送到服务器,用户的路径仍然可读。 – chris85

+0

您需要**尝试自己编写代码**。在[**做更多的研究**之后](https://meta.stackoverflow.com/q/261592/1011527)如果你有问题**发布你已经尝试了**的**明确的解释不工作**并提供[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。阅读[如何问](http://stackoverflow.com/help/how-to-ask)一个很好的问题。请务必[参观](http://stackoverflow.com/tour)并阅读[this](https://meta.stackoverflow.com/q/347937/1011527)。 –

回答

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]