我的网站最基本的形式是针对特定类型个人的网络站点。当个人注册时,我想创建一个自定义页面(或至少是一个图像/外观)来显示他们的个人资料。目前,我使用 PHP $_GET
参数来确定当用户导航到网站上的相应脚本时要显示哪个配置文件。
为了更好的SEO,我希望每个用户都有自己独特的链接;例如,FaceBook 允许其用户拥有可以链接的自定义 URL。如何在目录中为每个注册的新用户自动生成一个页面,并在用户更新其详细信息时自动更新该页面?据我所知,.htaccess 可能存在一个潜在的流程,使用用户的姓名/头衔/等从数据库中提取其详细信息,但将其显示为唯一的 URL。
任何人都可以提供建议或实现示例吗?我感谢任何和所有的帮助!
附注我想,一旦上述功能得到解决,根据用户位置(即按州)对文件进行排序应该不会那么困难。理想情况下,我希望有一个像 ../users/state/user_name 这样的目录结构,其中 state 是用户的位置,user_name 是他们的唯一名称。再次感谢!
最佳答案
apache 的 .htaccess 几乎没有什么工作 - 大部分逻辑都在 php 本身中。 Apache 只是将每个请求重定向到选定的脚本文件。
我假设您使用 apache wid mod_rewrite。如果是这种情况,您应该将其添加到 .htaccess 文件中:
# check if mod_rewrite is present
<IfModule mod_rewrite.c>
#turns it on
RewriteEngine on
#if the requested url isn't a file or a dir
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#process index.php, no matter what was in the url
RewriteRule ^ index.php [L]
</IfModule>
(请注意,.htaccess 还必须具有覆盖 apache 当前设置的权限)
在index.php中,您可以检查被隔离的网址并处理信息
$whatTheUserRequested=$_SERVER['REQUEST_URI'];
$parameterArray=explode('/', $whatTheUserRequested);
//check the first param.
switch($parameterArray[1])
{
//if it was something like "thesite.com/main/fsadf/hgfdsgsdf/...."
case "main":
include "mainPage.php";
//....or do something else
break;
//if it was something like "thesite.com/login/asdfe/xxxx/...."
case "login":
include "loginPage.php";
//....or do something else
break;
default:
//here you MUST check if $parameterArray[1] is a username
if(check in db if $parameterArray[1] is user)
{
include "userPage.php";
}
else
{
include "pageNotFound.html"
};
}
您还可以检查包含的 php 中的第三个或第四个参数,以处理像“mysite.com/johnny95/edit”这样的 url
关于php - 为新站点用户自动创建自定义页面(或 URL) - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275736/