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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么