Alternative URL in PHP

Asked

Viewed 161 times

2

I’m having a hard time creating an alternative, more user-friendly URL for my customers.

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

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

But in this case, what I’d like is when you type http://foo.bar/gladisonimoveis was directed to http://foo.bar/site.php?id=1042 only it gives error 404, because there is a directory /gladisonimoveis only that creating a directory for each client is something very exhaustive.

A fellow programmer of mine said I can standardize this by once configuring in htaccess.

I’m waiting for help, please. Rss

  • 2

    There is a resource called routing where you present user-friendly Urls and they are redirected to specific controllers in your application, this feature is usually available in MVC frameworks. You can search for PHP routing and see how to implement this manually if your case.

  • @Edsonhoraciojunior will do a search yes, thank you!

  • You can do this with htaccess yes. Do you work with Slug on the project? You can’t pass gladisonimoveis and know that it is id 1042, da para vc passar o gladisonimoveis e pesquisar por quem tem esse Slug, or then pass id after Slug, it would be gladisonimoveis/1042, but it would be better to pass this encrypted id.

2 answers

3

Create a file named after .htaccess and put at 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 site header. (Or in the appropriate location)

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]); 

substitute

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

for

http://foo.bar/site/1042
  • I did exactly as put here, but unsuccessfully!

  • Only using the . htaccess file and manually placing the URL in the browser as indicated should already work. The second part is to make the requisitions of the base, and it becomes difficult to explain without knowing the structure of the system. Try using this other example of http://answall.com/questions/112332/como-fa%C3%A7o-url-amig%C3%A1vel-no-php

  • What I have is a mysql with 3 fields: id, urllonga, urlcurta. I need that when I type urlcurta: http://www.roteirodoimovel.com.br/gladisonimoveis redirect to the long url: http://www.roteiromovel.com.br/site.php?id=1042. But all this dynamically, without I need to keep changing files to every new client that enters the system

  • With the instructions passed, you should ignore urlcurta and urlonga. Dynamically do . htaccess as explained.

-1

Take a look at this following material:

Creating a PHP 5 mini-framework with MVC

Credits to Matheus Moura.

In this video is explained the access to URL through the method GET, in addition to its formatting through the use of controllers and actions. In the other classes, the creation of a framework is focused mainly on access to the database through the PDO.

  • 3

    Leandro, although the link can answer the question, it is recommended to contextualize the content contained in it, so that the link serves as a reference only for the answer.

  • 2

    Okay, Diego, I admit it was a little poorly answered, I’ll change it.

  • 1

    Teaching how to build a framework is good to learn, but it’s reinventing the wheel. There are several frameworks on the market that have been doing this for years, such as Symfony (https://symfony.com/) for example.

  • I don’t disagree that is reinventing the wheel, is that frameworks are complete, and it just needs a user-friendly url system, it’s like it’s for a web developer, the netbeans is more than complete, but the sublime has everything you need, is that as I provided the first class of a series, I also highlighted what I had in the remainder of the series

Browser other questions tagged

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