使用用户名重写配置文件URL [英] Rewrite profile URL with username
问题描述
在使用mod_rewrite的代码中,我可以通过在域之后键入他的用户名来访问任何用户的个人资料,例如 example.com/john 这将带我们到john的个人资料。这很好用但是如果我输入任何不是我数据库中现有用户的用户名的名称,例如 example.com/notauser 我仍然会被重定向到空的个人资料页面,尽管它没有个人资料图片或其他信息,但所有其他按钮和链接,如粉丝,朋友等仍然可用。在这种情况下,我想重定向到登录页面或echo消息用户不存在,但无法弄清楚如何做到这一点。
In this code with mod_rewrite I can visit the profile of any user by typing his username after domain like example.com/john this will take us to john's profile. This works well however if I type any name which is not a username for existing user in my database like example.com/notauser I still get redirected to a empty profile page, though it has no profile pic or other info but all other buttons and links like followers, friends etc are still available. In such a case I want to redirected to login page or echo message user does not exist, but can't figure out how to do that.
<?php
if(!empty($_GET['username'])) {
$username = $_GET['username'];
}
else if(!empty($_SESSION['username'])) {
$username = $_SESSION['username'];
}
else {
header("Location: login.php");
die();
}
?>
< br $>
.htaccess文件
.htaccess file
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
推荐答案
_GET [' 用户名'])){
_GET['username'])) {
用户名 =
username =
_GET [' username'] ;
}
else if(!empty(
_GET['username']; } else if(!empty(
这篇关于使用用户名重写配置文件URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!