Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:59:10+00:00 2026-06-09T14:59:10+00:00

I am attempting to publish a question which I know has many answers floating

  • 0

I am attempting to publish a question which I know has many answers floating around Stack Overflow. However I, for some reason, just cannot seem to get the information in them to click. I will post my sources at the end of the question.

The question being, how can I pull a user_id from a URL & mod_rewrite it show the username.

Instead of this:

domain/user/index.php?user_id=1

I get this:

domain/username

I had customized other SO answers to my needs on my .htaccess file to this:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine On
  RewriteOptions MaxRedirects=1
  RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
  RewriteCond %{REQUEST_FILENAME} -d [NC] 
  RewriteRule .* - [L]
  RewriteRule ^user/([^/]+)$ index.php?user_id=$1
</IfModule>

The if statement at the top of my user page looks like this:

if (isset($_GET['user_id']) || isset($_GET['username']) && queryUserId($user_id)) {
  // Render Page Content
} else {
  header('Location: sign_up.php');
}

And my queryUsername functions looks like this:

function queryUsername($username) {
    $conn = dbConnect('read');
    $sql = "SELECT * FROM users WHERE user_id = '".$username."'";
    $result = $conn->query($sql) or die(mysqli_error($conn));
    $row = $result->fetch_assoc();
    return $row['username'];
    $username = $row['username'];
}

I have successfully implemented unique URL’s for users, and am accessing Account Profiles just fine when appending different user_id’s to my URL, so what am I missing to get the username written to the URL instead of variable strings and folder structure?

Cheers!

Other SO Questions:

Directly adding username to URL PHP

Get username from URL in PHP

Using mod rewrite to change URL with username variable

AddedBytes Article

URL Rewriting for Beginners

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:59:12+00:00Added an answer on June 9, 2026 at 2:59 pm

    This line is wrong according to your information:

    RewriteRule ^user/([^/]+)$ index.php?user_id=$1
    

    It should at least be:

    RewriteRule ^user/([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/user/username
    

    or using your description:

    RewriteRule ^([^/]+)$ /user/index.php?user_id=$1    // for a url like domain/username
    

    Also, you are not setting any variable with the name of username in the code you have shown, so the check for $_GET['username'] is unnecessary.

    Your check in php should look something like:

    if ( isset($_GET['user_id']) && queryUserId($_GET['user_id']) ) {
    

    Apart from that you should not use the deprecated mysql_* functions and use prepared statements as you have an sql injection problem now.

    Also note that using two return statements after each other only returns the first value.

    Edit: There seems to be some confusion between the user ID and the username. If the value in the url is a username, you’d better call it username in both the .htaccess file and php to avoid confusion with the user ID:

    RewriteRule ^([^/]+)$ /user/index.php?username=$1    // for a url like domain/username}
    

    and

    if ( isset($_GET['username']) && queryUserName($_GET['username']) ) {
    

    in the function (using the deprecated functions just to illustrate…):

    function queryUserName($username) {        
        $conn = dbConnect('read');
        $sql = "SELECT * FROM users WHERE username = '".$username."'";
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to build a C# project which has numerous references to assemblies in NuGet
I am attempting to publish a 3.5 MVC website on my build server through
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
I am currently attempting to use Visual Studio 2010 'Publish' and MSDeploy functionality to
Attempting to test whether a post has a 'main_slider', 'flickr-slider', or 'video-slider' value. 'main_slider'
I am attempting to grab all posts for a specific day (ie: today), however
Attempting to use a custom hex color for my css triangle (border). However since
Attempting to set the tabIndex for date_select has proven to be difficult out of
I am attempting to publish an MVC4 application from VS 2012 Pro running Windows
I am attempting to work out what is needed to publish a custom open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.