通常,显示某些个人资料页面的做法或非常古老的方式如下:
www.domain.com/profile.php?u=12345
u=12345
用户ID 在哪里.
近年来,我找到了一些非常好的网站,如:
www.domain.com/profile/12345
我如何在PHP中执行此操作?
就像一个疯狂的猜测,它是否与.htaccess
文件有关?你能给我一些关于如何写.htaccess
文件的技巧或示例代码吗?
根据这篇文章,你想要一个mod_rewrite(放在一个.htaccess
文件中)规则看起来像这样:
RewriteEngine on RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
这映射了来自的请求
/news.php?news_id=63
至
/news/63.html
另一种可能性就是这样做forcetype
,它强制任何特定路径下的任何东西使用php来评估内容.因此,在您的.htaccess
文件中,输入以下内容:
ForceType application/x-httpd-php
然后index.php可以根据$_SERVER['PATH_INFO']
变量采取行动:
我最近在一个适合我需求的应用程序中使用了以下内容.
的.htaccess
# 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]
的index.php
foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part) { // Figure out what you want to do with the URL parts. }
我试着在下面的例子中逐步解释这个问题.
0)问题
我试着问你这样:
我想打开像facebook profile www.facebook.com/kaila.piyush这样的页面
它从url获取id并将其解析为profile.php文件并从数据库返回featch数据并将用户显示给他的个人资料
通常当我们开发任何网站时,其链接看起来像www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678
现在我们更新新风格而不是重写我们使用www.website.com/username或example.com/weblog/2000/11/23/5678作为永久链接
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
在根文件夹中创建.htaccess文件或更新现有文件:
Options +FollowSymLinks # Turn on the RewriteEngine RewriteEngine On # Rules RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php
那是做什么的?
如果请求是针对真实目录或文件(服务器上存在的那个),则不提供index.php,否则每个url都会重定向到index.php.
2)index.php
现在,我们想知道要触发的操作,因此我们需要读取URL:
在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
现在在profile.php文件中,我们应该有这样的东西:
// 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:
http://www.domain.com/profile/12345/
有关更多htaccess的漂亮网址:http://www.webconfs.com/url-rewriting-tool.php