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