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

On my site, user profiles can be obtained using the URL www.example.com/user/profile.php?uid=3 I want to make it easier for users to reach their profile by simply asking www.example.com/username

Each user has a username that cannot be changed. 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] 
+4
source share
2 answers

Use this code in .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 at the username in the database table.

+7
source

If you have Apache , you must mod_rewrite .

Create a .htaccess file and upload it to the Apache root document and put this code:

 RewriteEngine on RewriteRule ^(.+)$ user/profile.php?username=$1 

This will cause the username to be user / profile.php with the username parameter, and you can get the username in the profile.php file and then make an SQL query to get the user profile.

+2
source

Source: https://habr.com/ru/post/1403474/


All Articles