0

I want to create a simple social network with PHP, that every user has a profile that share his image and text.

I have a problem that I want to add functionality in which users will the access to his profile like Facebook , Instagram and etc like below:

my website.com/user1

Not this : mywebsite.com/@user1

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Xander Cage
  • 51
  • 1
  • 3

2 Answers2

3

Create .htaccess file and add this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.

If you have any question about .htaccess file then please check this link.

MRustamzade
  • 1,425
  • 14
  • 27
0

For use with Nginx, refer to this answer https://stackoverflow.com/a/53415110/6749661

There are a few ways you can do this, like @MRustamzade's answer, you can change the .htaccess file to contain code like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /user.php?username=$1 [L] //update user.php?username to your users profile route.

You can also use a router framework, such as Altorouter which allows you to do this, and with a large number of pages.

With Altorouter you can set a number of routing rules, a simple setup could consist of:

$router = new AltoRouter();
$router->map( 'GET', '/profile/[*:username]', 'views/profile_view.php', 'home' );

$match = $router->match();

if($match) {
  header("HTTP/1.1 200 OK");
  require $match['target'];
} else {
  header("HTTP/1.0 404 Not Found");
  echo "Error 404 - File not found";
}

Using this code will allow you to go to site.com/profle/username will go to views/profile_view.php

In order to get the username from the URL in PHP, you can use the $match variable, something like this:

$username = $match["params"]["username"];
FluxCoder
  • 1,266
  • 11
  • 22