通常,显示某些个人资料页面的做法或非常古老的方式是这样的:
www.domain.com/profile.php?u=12345
u=12345
用户ID在哪里。
近年来,我发现一些网站的网址非常好,例如:
www.domain.com/profile/12345
我如何在 PHP 中做到这一点?
就像一个疯狂的猜测,它与.htaccess
文件有关吗?你能给我更多关于如何编写.htaccess
文件的提示或一些示例代码吗?
通常,显示某些个人资料页面的做法或非常古老的方式是这样的:
www.domain.com/profile.php?u=12345
u=12345
用户ID在哪里。
近年来,我发现一些网站的网址非常好,例如:
www.domain.com/profile/12345
我如何在 PHP 中做到这一点?
就像一个疯狂的猜测,它与.htaccess
文件有关吗?你能给我更多关于如何编写.htaccess
文件的提示或一些示例代码吗?
根据这篇文章.htaccess
,您需要一个看起来像这样的 mod_rewrite(放置在文件中)规则:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
这映射了来自的请求
/news.php?news_id=63
至
/news/63.html
另一种可能性是使用forcetype
,它强制任何东西沿着特定的路径使用 php 来评估内容。因此,在您的.htaccess
文件中,输入以下内容:
<Files news>
ForceType application/x-httpd-php
</Files>
然后 index.php 可以根据$_SERVER['PATH_INFO']
变量采取行动:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
我最近在一个可以很好地满足我的需求的应用程序中使用了以下内容。
.htaccess
<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>
索引.php
foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
// Figure out what you want to do with the URL parts.
}
I try to explain this problem step by step in following example.
0) Question
I try to ask you like this :
i want to open page like facebook profile www.facebook.com/kaila.piyush
it get id from url and parse it to profile.php file and return featch data from database and show user to his profile
normally when we develope any website its link look like www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678
now we update with new style not rewrite we use www.website.com/username or example.com/weblog/2000/11/23/5678 as permalink
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Create a .htaccess file in the root folder or update the existing one :
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What does that do ?
If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.
2) index.php
Now, we want to know what action to trigger, so we need to read the URL :
In index.php :
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Now in the profile.php file, we should have something like this :
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
简单的方法来做到这一点。试试这个代码。将代码放入您的htaccess
文件中:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*)/ profile.php?u=$1
RewriteRule profile/(.*) profile.php?u=$1
它将创建这种类型的漂亮 URL:
更多 htaccess 漂亮网址:http ://www.webconfs.com/url-rewriting-tool.php
它实际上不是 PHP,它是使用 mod_rewrite 的 apache。发生的情况是该人请求链接 www.example.com/profile/12345 然后 apache 使用重写规则将其切碎,使其看起来像这样 www.example.com/profile.php?u=12345 到服务器。你可以在这里找到更多:重写指南
There are lots of different ways to do this. One way is to use the RewriteRule techniques mentioned earlier to mask query string values.
One of the ways I really like is if you use the front controller pattern, you can also use urls like http://yoursite.com/index.php/path/to/your/page/here and parse the value of $_SERVER['REQUEST_URI'].
You can easily extract the /path/to/your/page/here bit with the following bit of code:
$route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
From there, you can parse it however you please, but for pete's sake make sure you sanitise it ;)
ModRewrite 不是唯一的答案。您还可以在 .htaccess 中使用 Options +MultiViews,然后检查$_SERVER REQUEST_URI
以查找 URL 中的所有内容。
看起来您正在谈论 RESTful Web 服务。
http://en.wikipedia.org/wiki/Representational_State_Transfer
.htaccess 文件确实重写了所有 URI 以指向一个控制器,但这比您现在想要的更详细。你可能想看看Recess
这是一个 RESTful 框架,全部使用 PHP