The URL, which is used by SO, is very handy, for example if a question is posted with the same title as another existing question, the title path when passed to the URL may be the same, but StackOverflow adds an ID to each post that makes the difference of the questions.
Ex:
https://es.stackoverflow.com/questions/179431/consulta-basica-de-pdo
Looking at the URL, we see the next ID 179431
generated in the question.
Based on the URL example, how can I use the same layout, the same system in my news app, to get the following friendly route:
example.com/noticias/171210/mundial-rusia-2018/
My table layout 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 the unique IDs of the news?
- How to generate the unique ID automatically for each news post?
What changes should I use, in my
.htaccess
to get the friendly URL?RewriteRule ^noticias/([a-zA-Z0-9-/]+)$ all_sport.php?id=$1
Your table should have a
id
unique field that would be the primary key of the table. Many times this value is usually autoincremental so you don't have to worry about filling it in yourself.That
id
will be the one you use in your url. What comes after is really not important to do the redirect. Even right here on Stack Overflow. Try modifying this same url by changing the "title" at the end. You'll see that reloading loads the question it touches and restores the title. This is because what really matters is the id. It uses that id to retrieve the question (and its title among other things) and then uses the title to populate 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 for news, another for world...) because then the id refers to the previous table.
So that:
Modify the table(s) so that the primary key is an id. Make it auto-incremental
Use the following rule in the htaccess:
If you have it all in one table:
By cons, if there are different tables:
In both cases we ignore what comes after the id because it might not be there or because it might have been changed by the user. When we retrieve the database title we will put it in the url.
Within your php, based on the data you receive, you should do a database query and retrieve the record. You will also recover a title.
You will use the retrieved title to create a new url-friendly title for yourself. You can do this with preg_replace and one of the things you should do is change the blanks to hyphens.
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 retrieved and modified. You'll also need to do it with preg_replace.
Depending on how you do it, it could happen that updating the url will (uselessly) reload the page. But it can be avoided. This Stack Overflow question talks about how to update the url without reloading the page.
Finally, when you create links to other pages
<a href...
remember to put the same format (id + modified title)You can use this:
I hope it helps you.
The objective is to create a url of the style example.com/noticias/171210/world-russia-2018/
First you must generate the friendly links, for this you must do the following:
In this way you will generate your friendly url, which will be in the links of each news. Now your questions: - What column and what type should I add in my table for the unique IDs of the news? The ID can be the same one you already have (of type Int), you just have to make it non-null, and have it auto-increment
How to generate the unique ID automatically for each news post? The ID will auto-increment as you create news, at the time of making the insert in your database it will generate the new ID for each news.
What changes should I use in my .htaccess to get the friendly URL?
In this way you will receive in all_sport.php the Id of the news and already carry out your query in the database