php – How to create a unique ID of a post similar to StackOverflow?

Question:

The URL, which uses SO, is very practical, for example if a question is published with a title equal to another existing question, the path of the title when passing to the URL may be the same, but StackOverflow adds an ID to each publication that makes the difference of the questions.

Ex:

https://es.stackoverflow.com/questions/179431/consulta-basica-de-pdo

Looking at the URL, you can see the following ID 179431 generated in the question.

Based on the example of the URL, how can I use the same design, the same system in my news application, in order to obtain the following friendly route:

example.com/noticias/171210/mundial-rusia-2018/

The layout of my table is structured as follows:

id_mini_cover    cover_page    title    description    url    ...
1                17382.jpg     ...      ...            ...    ...

Questions

  • What column and what type should I add in my table for unique news ids?
  • How to generate the unique ID automatically for each publication of a news?
  • What changes should I use in my .htaccess to get the friendly URL?

     RewriteRule ^noticias/([a-zA-Z0-9-/]+)$ all_sport.php?id=$1

Answer:

Your table should have a unique id field, which would be the primary key of the table. Many times this value is usually self-incrementing so you don't have to worry about filling it in yourself.

That id will be the one you use in your url. What follows is not really important to do the redirect. Even right here on Stack Overflow. Try to modify this same url by changing the "title" at the end. You will see that when reloading the question that touches is loaded and restores the title. This is because what really matters is the id. Use that id to retrieve the question (and its title among other things) and then use the title to complete it in the browser bar.

Only, in addition to the id, you might need the first part: 'news'. If you are working with different tables (one of the news, another of the world …) because then the id refers to the previous table.

So that:

  1. Modify the table / s so that the primary key is an id. Make it auto incremental type

  2. Use the following rule in the htaccess:

    If you have it all in one table:

     RewriteRule ^noticias/([^/]+)/?$ all_sport.php?id=$1

    By cons, if there are different tables:

     RewriteRule ^([^/]+)/([^/]+)/?$ all_sport.php?id=$2&cat=$1

    In both cases we ignore what comes after the id because it could be that it is not there or because it could have been modified by the user. When we retrieve the database title we will put it in the url.

  3. Within your php, based on the data you receive, you will have to do a database query and retrieve the record. You will also recover a title.

  4. You will use the retrieved title to create a new url friendly title. You can do this with preg_replace and one of the things you should do is change the blanks to hyphens.

  5. You will update the url with the title. To do this you must remove the previous title (if any) and add the one you just recovered and modified. You will also have to do it with preg_replace.

  6. Depending on how you do it, it could happen that updating the url will reload the page (uselessly). But it can be avoided. This Stack Overflow question talks about how to update the url without reloading the page.

  7. Finally, when you create links to other pages <a href... remember to put the same format (id + modified title)

Scroll to Top