On my site, user profiles can be reached by the url www.example.com/user/profile.php?uid=3
I want to make it easier for users to reach their profile by simply requesting for www.example.com/username
Each users has a username that cannot change. How can I do this?
Here is my current .htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# finally this is your rewrite rule
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
RewriteRule ^(.+)$ user_page/profile.php?uid=$1 [L,QSA]
Use this code in your .htaccess
under DOCUMENT_ROOT
:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]
# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
# forward /john to user_page/profile.php?name=john
RewriteRule ^((?!blog/).+)$ user_page/profile.php?name=$1 [L,QSA,NC]
Now inside profile.php
you can translate $_GET['name']
to $uid
by looking up user's name into a database table.
If you have Apache you should mod_rewrite module.
Create a .htaccess file and upload it on Apache Document Root and put this code:
RewriteEngine on
RewriteRule ^(.+)$ user/profile.php?username=$1
This will pass the username to user/profile.php with username parameter and you can get the username in profile.php and then make your SQL query to get the user profile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With