Alternative URL in PHP

1

I'm having a hard time creating an alternative, friendlier URL for my clients.

The real estate system that I have generates an alternate address for each of them, for example: http://foo.bar/site.php?id=1042

However, I need the URL to be more user friendly, that is, simple to store. Thinking about this I generated in the system the creation of URLs for clients of type http://foo.bar/gladisonimoveis

But in this case, what I would like is that when typing http://foo.bar/gladisonimoveis is directed to http://foo.bar/site.php?id=1042 it only gives 404 error, since there is no / gladisonimoveis directory, but to create a directory for each client is something very exhausting.

A programmer colleague of mine said that I can standardize this by setting once in htaccess.

I'm waiting for help, please. Rss

    
asked by anonymous 23.02.2016 / 13:20

2 answers

2

Create a file named .htaccess and place it in the root of the site.

RewriteEngine on
Options +MultiViews

#pagina de errro
ErrorDocument 404  /404.php  

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Create a condition to separate the strings from the URL and place them in the header of the site. (Or in the local area)

Example:

//Obtem a URL
$aux = substr( $_SERVER['REQUEST_URI'], strlen('/')); 
if( substr( $aux, -1) == '/'){ 
  $aux=substr( $aux, 0, -1); 
} 

//Separa a URL
$urlARRAY___ =explode( '/', $aux); 

$id  = mysqli_real_escape_string($conexao, $urlARRAY___[2]); 

replace

http://foo.bar/site.php?id=1042

by

http://foo.bar/site/1042
    
23.02.2016 / 14:32
-1

Take a look at this material:

Creating a PHP 5 mini framework with MVC

Credits to Matheus Moura.

This video explains access to the URL using the GET method, as well as its formatting through the use of controllers and actions. In other classes, it is focused the creation of a framework mainly for access to the database through PDO .

    
23.02.2016 / 14:07