Click here to Skip to main content
15,031,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code with mod_rewrite I can visit the profile of any user by typing his username after domain like 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 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.

if(!empty($_GET['username'])) { 
$username = $_GET['username'];
else if(!empty($_SESSION['username'])) { 
$username = $_SESSION['username'];
else { 
header("Location: login.php"); 

.htaccess file

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
Updated 8-Jul-18 9:27am

1 solution

There is no error in your code as the script is instructed to only redirect when no username is provided.
The code should otherwise say, redirect when username supplied is not found in database.
That way you will get good results.

Funny i stumbled onthis code while trying to google coreect way to implement htaccess for pretty links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900