Url Friendly with Mysql

3

I'm building a website that pulls ids from the database and makes the page of the post, but when it asks the url it stays as id like this:

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

I wanted instead of showing the id, show the Title of the post that I get in the database, for example, www.mysite.com/Como_Criar_URls_amigaveis without needing to appear the posts.php? id, thank you to help.

    
asked by anonymous 19.07.2015 / 21:40

3 answers

3

Firstly, what you say is to show the ID, in fact it's not quite like that, your url represents a file on your server, and the ID represents a parameter passed to that page. When you press ENTER on the bar of your browser, it is performing an HTTP GET request to your server, passing that ID parameter.

On this page, there is probably a code like this:

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

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

Imagine if you changed the ID parameter by TitlePost, your code would probably look like this:

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

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

link

But let's say you edit the title of the post to

"How to CreateLinkAmigavel"

You would have a problem, because the redirect button is set with the parameter tituloPost="How to CreateVariousLinkAmigavel".

So the maintainability of your site is extremely low.

Another situation would be more of 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 impractical.

Is it possible to create friendly links?

Yes, it is possible to create more friendly links, however, it is strongly recommended to use the ID as a parameter, since the ID, besides being unique, represents a safe value of changes, that is, you can edit your post, title, tags, that ID will continue the same.

    
19.07.2015 / 22:54
2

What you describe is a desire many people have when creating a website. The answer from Fernando Medeiros is quite useful and simple, basically speaking use the ID, do not worry .

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

But how do other people do? How many sites can have very practical and user-friendly URLs, but also that they can keep them easy, overcoming the risks that Fernando menciunou? And, indeed, they are serious annoyances that can cost a lot!

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

Basically, my "answer" would be, do not try to reinvent the wheel † , ie do not try to create everything from scratch, but rather, use a framework that already solved all these problems. >

† I do not know if this expression we have in English does the same in Portuguese: P

    
20.07.2015 / 02:19
1

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

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

// OBS: talvez precise alterar algum detalhe

So, the url:

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

They 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 title of the post without breaking the old title links.

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

slug('Título do post') = titulo-do-post
    
20.11.2016 / 03:45