Mysql Friendly URL

Asked

Viewed 496 times

3

I’m building a site that pulls ids from the database and makes the post page, but when it makes the request the url gets like id like this:

http://www.meusite.com.br/posts.php?id=2

Would you like instead of showing the id, to show the post title that I picked up in the database, for example, www.meusite.com.br/Como_cria_urls_amigaveis without having to show the posts.php? id, I appreciate the help.

  • Thank you, really rebuild the system of more work, changed the way, using Wordpress now.

  • Still it would be interesting to vote and accept the answer that best solved the problem...

3 answers

3

First, what you say is to show the ID, actually it is not so, your url represents a file on your server, and the ID represents a parameter passed to that page. When you press ENTER in your browser bar, it is performing an HTTP GET request to your server, passing this ID parameter.

On this page, there’s probably a code like this:

Select * from Posts where postID = $_GET["ID"]

That is, "Bring the POST where the ID is equal to the Parameter passed in the request"

Imagine if you swapped the ID parameter for Titulopost, your code would probably look like this:

Select * from Posts where titPost = $_GET["TituloPost"]

Now think to me, you create a button on a certain part of your site, which redirects to that your post, for example, when someone clicks on a link, it opens the address;

http://www.seublog.com.br/posts.php?tituloPost=ComoCriarVariosLinkAmigavel

But let’s say you edit the post title to

"How to create friendliness"

You would have a problem because the redirect button is set to the titlePost parameter = "How to create varioslinkamigavel".

So the maintainability of your site is extremely low.

Another situation would be more than a post having the same title, which would break the functioning of the site, where each page shows a post.

That is, there are several situations that make this practice unfeasible.

You can create friendly links?

Yes, it is possible to create friendlier links, however, it is totally recommended to use the ID as a parameter, because the ID is not only unique, it represents a safe value of changes, that is, you can edit your post, title, tags, that the ID will remain the same.

  • I understood then, the correct thing would be to create an sql that searches based on the id number and the post name, however, after that, how do I put my htacces?

  • 1 'Maintainability' is not linked to the user-friendly url system, this is extremely simple to do. 2 totalmente recomendado o uso do ID como parâmetro - totally wrong.

  • Please write your reply

  • @Fernandomedeiros, I already have several on the subject

2

What you describe is a desire that many people have when creating a site. Fernando Medeiros' answer is quite useful and simple, basically speaking use the ID, don’t worry.

I agree with that, and I think in the case of posts, the ID in the URL is practical and easy.

But how do other people do it? How many sites can have very practical and amicable Urls, but also that can keep them easy, overcoming the risks that Fernando menciunou? And, really, they are serious troubles that can cost a lot!

A common developer like me uses a framework already established. In the case of posts and PHP and Mysql, there is the Wordpress. Wordpress can keep your Urls friendly using the database, and an algorithm to "find" the nearest URL if it’s not an exact url.

Basically, my "answer" would be, doesn’t try to reinvent the wheel, that is not trying to create everything from scratch, but rather, it uses a framework that has solved all these problems.

† I don’t know if this expression we have in English gives the same in Portuguese :P

  • Wordpress can be a good output even

  • Sure enough, I tried to reinvent the wheel thinking it was easy, from work.

1

A simple way would be to change your .htaccess for something like:

RewriteEngine On
RewriteRule   ^post/([0-9]+)/(.*)?$   posts.php?id=$1   [NC,L]

// OBS: talvez precise alterar algum detalhe

Thus, the url:

http://www.meusite.com.br/post/22/titulo-do-post
http://www.meusite.com.br/post/22/titulo-do-post-editado

Would be interpreted as:

http://www.meusite.com.br/posts.php?id=22

That is, the title would be used to make the url friendly and improve SEO. In the background what would matter for correct display would be the ID. This allows you to change the post title without breaking the links with old title.

It is up to you to generate the urls in your application, remembering to generate this Slug of the post title.

slug('Título do post') = titulo-do-post

Browser other questions tagged

You are not signed in. Login or sign up in order to post.