Rewrite profile URL with username.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harman30
    New Member
    • Sep 2015
    • 19

    Rewrite profile URL with username.

    In this code with mod_rewrite I can visit the profile of any user by typing his username after domain like example.com/john this will take us to john's profile. This works well however if I type any name which is not a username for existing user in my database like example.com/notauser I still get redirected to a empty profile page, though it has no profile pic or other info but all other buttons and links like followers, friends etc are still available. In such a case I want to redirected to login page or echo message user does not exist, but can't figure out how to do that.

    profile.php
    Code:
    <?php
    if(!empty($_GET['username'])) { 
    $username = $_GET['username'];
    }
    else if(!empty($_SESSION['username'])) { 
    $username = $_SESSION['username'];
    }
    else { 
    header("Location: login.php"); 
    die();
    }
    ?>
    .htaccess file
    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if your database query returns an empty result, redirect to the login/error page.

    Comment

    Working...