Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I achieve a url like www.example.com/username to redirect to www.example.com/user/profile.php?uid=10? [closed]

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]
like image 801
Chibuzo Avatar asked Jan 17 '23 02:01

Chibuzo


2 Answers

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.

like image 146
anubhava Avatar answered Jan 29 '23 21:01

anubhava


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.

like image 39
Afshin Mehrabani Avatar answered Jan 29 '23 20:01

Afshin Mehrabani