【问题标题】:display username in url instead of id using .htaccess使用 .htaccess 在 url 中显示用户名而不是 id
【发布时间】:2014-02-08 17:52:53
【问题描述】:

我已经在寻找这个How to create user profiles with PHP and MySQL 和这个profile username in URL instead of user ID 以及其他。但它仍然不适合我。我当前的url 当用户查看他/她的个人资料时是这样的http://localhost/sn/profile.php?id=1 现在我想要url 是这样的http://localhost/sn/myusername 我知道这可能是重复的但仍然没有任何答案到目前为止为我工作:(有什么帮助吗?提前谢谢。

这是我在.htaccess中尝试过的

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/$ http://localhost/sn/profile.php?id=$1

【问题讨论】:

  • 您的 htaccess 在哪里?在您的sn 目录或文档根目录中?
  • 它与我的index.phpprofile.php 一起位于sn 文件夹内

标签: php apache .htaccess mod-rewrite


【解决方案1】:

您的 htaccess 代码应如下所示:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?id=$1 [L]

请注意,您必须更改profile.php 中的逻辑以通过用户名(不再是ID)获取用户

【讨论】:

  • 那位先生是什么意思?
  • 一个例子:你想到达http://localhost/sn/Pengun,它在内部被重定向到profile.php?id=Pengun。之前,您的 php 页面接收 ID,但现在它是用户名
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多