doukong9982 2015-11-11 00:48
浏览 29
已采纳

使用.htaccess将用户ID转换为用户名?

I want to use .htaccess to convert the urls that link to profile pages from an ID number to a name. For example, on Facebook, if you were to go to:

https://facebook.com/profile.php?id=4

It would take you to Mark Zuckerberg's profile page. However, you can also simply go to:

https://facebook.com/zuck

...which is more user friendly and easier to remember. How can I use htaccess to convert an id number to a username? Do I need to send it to a php script to process and send back the name?

  • 写回答

2条回答 默认 最新

  • dongtiaobeng7901 2015-11-11 01:03
    关注

    You can't specifically with .htaccess because you'd need to grab the username for that ID from your database, however you CAN use PHP to do so. In your .htaccess file:

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

    Then, you can check something like this in profile.php:

    if(isset($_GET['user'])){
        if(is_numeric($_GET['user'])){ //Check if the input is all numeric
            //Grab the username from database
            //Redirect user to site.com/user/$username
        }
    }
    

    More info on PHP's is_numeric() function: http://php.net/manual/en/function.is-numeric.php

    You might want to also check if the user's username is completely numeric, to avoid constantly redirecting the user. If you make sure users don't have all numeric usernames, you'll be fine, though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码