简体   繁体   中英

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?
Can someone help me with the codes and the .htaccess configuration?



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


If my user wants to view his page, he's gonna go to " users.com/hisusername ". The link is gonna his username and displays information about his account, and the gonna have the re-write rule (I guess?). 他的用户名并显示有关他的帐户的信息, 将具有重写规则(我猜是吗?)。 Can someone help me with the code?


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 :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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