简体   繁体   English

如何基于帐户的用户名添加URL

[英]How to add a URL based on the username of the account

How do I supposed to create a url based on the username of the user? 我应该如何根据用户名创建一个URL?
Can someone help me with the codes and the .htaccess configuration? 有人可以帮助我提供代码和.htaccess配置吗?

users.com/usernamehere users.com/username此处

If you want to view it on Github 如果要在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 ". 如果我的用户想要查看其页面,则将转到“ 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?). 该链接将获取他的用户名并显示有关他的帐户的信息, .htaccess将具有重写规则(我猜是吗?)。 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. 我已经在一些教程中看到了这一点,但我无法对其进行跟踪,因此,如果页面上带有“ ?username=usernamehere ”标签,则将在“ ?username=usernamehere usernamehere ”页面上重定向usernamehere
Example: The user is on " users.com/profile.php?username=Stackoverflow " and the user is gonna be redirected to: " users.com/users/Stackoverflow ". 示例:用户位于“ users.com/profile.php?username=Stackoverflow ”上,并且该用户将被重定向到:“ users.com/users/Stackoverflow ”。 Any chance of this gonna happen? 这有可能发生吗? Thanks and have a good day :) 谢谢,祝你有美好的一天:)

The user is on users.com/profile.php?username=Stackoverflow and the user is gonna be redirected to: users.com/users/Stackoverflow . 该用户位于users.com/profile.php?username=Stackoverflow并将该用户重定向到: 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. 注意:尽管此引用的含义相反,但以下答案假定您希望用户在URL栏中输入users.com/users/Stackoverflow

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 将一个名为.htaccess的文件放在您网站的文档根目录中(包含您的网站文件的最高目录,通常称为public_htmlwww )。添加以下规则

RewriteEngine on
RewriteBase /

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

The NC flag makes the comparison case-insensitive. NC标志使比较不区分大小写。 Meaning that users/Stack and USERS/Stack will both be redirected. 这意味着users/StackUSERS/Stack都将被重定向。 Remove it if you want a case-sensitive comparison 如果要区分大小写的比较,请将其删除

The END flag tells the server to stop looking for more redirect rules. END标志告诉服务器停止寻找更多的重定向规则。 Basically it ensures that /profile.php?username=... be the final destination of that request. 基本上,它确保/profile.php?username=...是该请求的最终目的地。

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 如果路径/users/name不是真实文件,则可能会导致问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM