How to build friendly URLs, such as Facebook and Twitter.

3

I have a question, how can I build links like Facebook and Twitter? For example: http://facebook.com/nome-qualquer .

I know that .htaccess treats the url in some way, but the question I'm beating is how to load the contents of url http://fb.com/fulano-de-tal

I get everything after the last bar and a query is done in SQL with fulano-de-tal and then the result is returned?

    
asked by anonymous 13.03.2014 / 21:21

2 answers

2

In your HTACCESS you should define something like

RewriteEngine on
RewriteRule ^([a-z]+)$ profiles.php?name=$1

Then in your profiles.php file you retrieve the $ name parameter and search the information in the database for that person you want to display on that page

>

so your url would look something like

www.seusite.com/tchicotti

But in fact your site would be accessing this way:

www.yoursite.com/profiles.php?name=tchicotti

    
13.03.2014 / 21:31
1

Routes

What you are trying to do is what we call routes today.

When you use some Framework, most of them already contain this functionality by default, making route management much easier.

Nothing prevents you from creating a .htaccess and defining your routes, but is less user-friendly and can make maintenance difficult.

You can create in .htaccess a route so that everything after the bar ( site.com.br/ ) is directed to a specific file, and in that file you can do the treatment you want, either with a simple if or something more sophisticated.

Here's an example:

.htaccess

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

// tudo que cair em /usuarios/usuario-x => será redirecionado para usuarios.php
RewriteRule ^usuarios/(\d+)*$ ./usuarios.php?id=$1

// o mesmo acontece com essas \/
RewriteRule ^posts/(\d+)*$ ./usuarios.php?id=$1
RewriteRule ^comunidades/(\d+)*$ ./comunidades.php?id=$1

users.php

<?php
    // aqui você irá encontrar os dados e tratar da forma que desejar.
    print_r($_SERVER['REQUEST_URI'])
?>

See how the routing system works for some Frameworks:

13.03.2014 / 22:41