Creating dynamic auto-incrementing seo-friendly url from php and sql

By clicking on John Smith user at user_id 1, he should go to the URL www.example.com/John-Smith, and not to profile.php? uid = 1

When you click on user John Smith in user_id 2, should he go to the URL www.example.com/John-Smith-2 profile.php? UID = 2

When you click on Kia Dull user in user_id 3, should he 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 a .Htaccess file and php / sql for this.

When I click on a user profile, I just simply bring it here.

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

which does nothing.

and here is my .htaccess

 RewriteEngine on RewriteBase / RewriteRule ^(.*)$ profile.php?uid=$1 [L] 
+4
source share
6 answers

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

SEO friendly URLs with PHP

+1
source

I would create another column in the database called "slug" and make it a unique row. Now, adding a new entry, I would generate a slug for this.

I found an example for a bullet creation function as an answer to this question: PHP function to create a pool (URL string)

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

If you did all this, your data should look something like 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 following steps should be fairly easy.

This will be the ability to create links:

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

The .htaccess file should be the same as yours already ...

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

And you just need to query the table for a value like $ _GET ['uid'] in the profile.php file. Now I renamed the uid parameter to slug because it is no longer an ID, but you can easily get the identifier by requesting it after the bullet.

The advantages of this solution:

  • Easy to debug. Slug is what you can find in a 1: 1 database. If you have a URL, you can tell without knowing which user it is.
  • According. Even if you delete the line with id = 1, the line id = 2 will still have the pool "john-smith-1"

FYI:

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

+1
source

You missed some code, the exact code looks 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> 

create the url you want and make changes to HTACCESS to match the url if necessary.

0
source

You must use echo to display the data in these links,

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

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

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

And something like this for your reference.

 <a href="<?=$row['first_name']?>-<?=$row['last_name']?>-<?=$row['user_id']?>">User</a> 
0
source
 RewriteEngine on RewriteBase / RewriteRule ^([^.*]+)$ profile.php?uid=$1 

Gotta do the trick

-1
source

All Articles