4

Clicking on user John Smith in position user_id 1 It should go to the url www.example.com/John-Smith as opposed to profile.php?uid=1

When you click on user John Smith in user_id 2 It should go to the url www.example.com/John-Smith-2 profile.php?uid=2

When you click on user Kia Dull in user_id 3 It should go to the url www.example.com/Kia-Dull profile.php?uid=3

Table Users

User_id    First_name    last_name
   1          John         Smith
   2          John         Smith
   3          Kia          Dull

How do I format my .Htaccess file and php/sql for this.

When a user profile is clicked I just simply lead it here.

<a href="<?php echo $row[first_name] ?>-<?php echo $row[last_name] ?>"

which doesn't do anything.

and here's my .htaccess

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ profile.php?uid=$1 [L]
thedullmistro
  • 382
  • 7
  • 20

6 Answers6

1

Have a look at following article. Hope it describes clear/simple and with example what you need.

SEO Friendly URLs with PHP

Sami
  • 8,168
  • 9
  • 66
  • 99
1

I would create another column in the database called something like "slug" and make it a unique string. Now, when adding a new record, I'd generate the slug for this one.

I found an example for a function to create the slug as an answer to this question: PHP function to make slug (URL string)

The slugify-function, provided in the answer to the quoted question, does not check if the value already exists in your database. You'd have to do it yourself and add a suffix like "-1" if the slug already exists.

If you have done all that, your data should look similar to this:

Table Users

User_id    First_name    Last_name     Slug
   1          John         Smith     john-smith
   2          John         Smith    john-smith-1
   3          Kia          Dull       kia-dull

The next steps should be quite easy.

This would be an option to create the links:

<a href="<?php echo $row['Slug'] ?>"><?php echo $row['First_name'] ?> <?php echo $row['Last_name'] ?></a>

The .htaccess file should be fine like you have it already ...

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ profile.php?uid=$1 [L]

And you just have to query the table for a value like $_GET['uid'] in your profile.php file. Now I'd rename the parameter uid into slug, because it's not longer an ID, but you can get the ID quite easily by querying after the slug.

Benefits of this solution:

  • Easy to debug. The slug is something you can find 1:1 in the database. If you now have a URL you can tell, without guessing, which user it is.
  • Consistent. Even if you'd delete the row with id=1, the row id=2 would still have the slug "john-smith-1"

FYI:

A slug is the part of a URL which identifies a page using human-readable keywords. http://en.wikipedia.org/wiki/Clean_URL#Slug

Community
  • 1
  • 1
SimonSimCity
  • 6,415
  • 3
  • 39
  • 52
0

You Missed Little bit of code the exact Code is like this...

<a href="<?php echo $row['first_name']."-".$row['last_name']."-".$row['User_id']."profile.php?uid=".$row['User_id']; ?>"><?php echo $row['first_name']."-".$row['last_name']."-".$row['User_id']; ?></a>

this creat the URL which you want and do changes in HTACCESS as per the URL if required.

JIT1986
  • 682
  • 6
  • 9
0

You must use the "echo" to display the data in that links,

<a href="<?php **echo** $row[first_name] ?>-<?php **echo** $row[last_name] ?>"
charles
  • 101
  • 5
0

Use this for your htaccess rules. The rule basically matches anything that ends with - followed by numbers

RewriteEngine on
RewriteBase /
RewriteRule -([\d]+)$ profile.php?uid=$1 [L]

And something like this for your link.

<a href="<?=$row['first_name']?>-<?=$row['last_name']?>-<?=$row['user_id']?>">User</a>
Ozzy
  • 10,285
  • 26
  • 94
  • 138
-1
RewriteEngine on
RewriteBase /
RewriteRule ^([^.*]+)$ profile.php?uid=$1

Should do the trick

Terry
  • 332
  • 1
  • 15