dtczp02204 2016-07-17 06:01
浏览 135

如何根据帐户的用户名添加URL

How do I supposed to create a url based on the username of the user?
Can someone help me with the codes and the .htaccess configuration?

users.com/usernamehere

If you want to view it on Github
If you want to view it on a live website

Info
If my user wants to view his page, he's gonna go to "users.com/hisusername". The link is gonna get his username and displays information about his account, and the .htaccess gonna have the re-write rule (I guess?). Can someone help me with the code?

Suggestion
I've saw this in some tutorials but I can't follow them, so if the page have the tag of "?username=usernamehere", the user is gonna be redirected on the "usernamehere" page.
Example: The user is on "users.com/profile.php?username=Stackoverflow" and the user is gonna be redirected to: "users.com/users/Stackoverflow". Any chance of this gonna happen? Thanks and have a good day :)

  • 写回答

1条回答 默认 最新

  • dp5657087 2016-07-17 06:18
    关注

    The user is on users.com/profile.php?username=Stackoverflow and the user is gonna be redirected to: users.com/users/Stackoverflow. Any chance of this gonna happen?

    Note: Although this quote suggests the opposite, the answer below assumes that you want the user to enter users.com/users/Stackoverflow in the URL bar.

    Place a file called .htaccess in your website's document root (the highest directory that contains your website files, typically called public_html or www) Add the following rules

    RewriteEngine on
    RewriteBase /
    
    RedirectRule ^users/(.*)  /profile.php?username=$1  [END,NC]
    

    The NC flag makes the comparison case-insensitive. Meaning that users/Stack and USERS/Stack will both be redirected. Remove it if you want a case-sensitive comparison

    The END flag tells the server to stop looking for more redirect rules. Basically it ensures that /profile.php?username=... be the final destination of that request.

    addendum

    If you really want to redirect customers in the opposite directions, use this rule instead

    RedirectRule ^profile.php?.*username=([^&$]+)  /users/$1  [END,NC]
    

    This will probably cause you problems though if the path /users/name is not a real file

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题