URL Friendly by title

2

Good evening people,

I am here implementing the friendly url system on a website and I already have to work for some page, I am now blocked in the file view_establishment which is what shows the establishments in this case I have for example an establishment to list thus http://exemplo.pt/ver_estabelecimento&id=1 o which I intend and replace with% w / o% where the field name of the establishment will come from the database.

I have the .htaccess file sealed like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?m=$1

And I have my file funcoes.php which is where I have the rules to incorporate the pages and show the content my problem is here

$url = $_GET['m'];
$urlSepara = explode('/',$url);

$primeiraUrl = $urlSepara[0];
$directorio = ("conteudos");

$permissao = array('home', 'comer', 'dormir', 'comprar', 'servicos', 'lazer', 'visitar', 'contactos', 'login', 'erro');

if(!isset($primeiraUrl) || $primeiraUrl == ''){
    include("conteudos/home.php");
}elseif(isset($primeiraUrl) && in_array($primeiraUrl, $permissao)){
    include("".$directorio."/".$primeiraUrl.".php");
}

Thank you for your help

    
asked by anonymous 08.02.2015 / 02:28

1 answer

1

It may be that your 'problem' is more a design issue.

See that your permission array does not match, so you need to get the 1st bar that corresponds to a file that will scan bar by bar with a series of if's.

$permissao = array( 'home' , 'comer' , 'dormir' , '...' );

if(!isset($primeiraUrl) || $primeiraUrl == ''){
    include("conteudos/home.php");
}elseif(isset($primeiraUrl) && in_array($primeiraUrl, $permissao)){
    include("".$directorio."/".$primeiraUrl.".php");
}

This form is very flexible, the ideal is to create a config file that abstracts the routes and a file to validate.

If the URL is http://exemplo.pt/empresa-x , then its $primeiraUrl variable will have the company-x value, and this is outside its rules in the $permissao array.

One solution that would solve your problem would be to treat the exception as an ad page. This way,% s of% URLs will be accepted and it will be up to the system to check if the ad was found or not and will continue to work for other URL's you have, type http://exemplo.pt/qualquer-nome-de-empresa , http://exemplo.pt/comer/ .

solution

if( ! isset( $primeiraUrl ) || $primeiraUrl == '' ) {
    include 'conteudos/home.php';
} elseif( in_array( $primeiraUrl , $permissao ) ) {
    include "{$directorio}/{$primeiraUrl}.php";
} else {
    // $primeiraUrl não está no array, posso SUPOR que seja um anuncio
    // include no arquivo que carrega o anuncio
    // include 'anuncio.php';
}

ad.php

Verifica no DB se '$primeiraUrl' corresponde a algum anúncio
caso contrário, você apresenta sua mensagem de erro.404

OBS , this solves the problem but is not ideal. I recommend looking for routes in PHP

Update

By doing .HTACCESS routing, you would have to define all possible routes, including order, as in the example below.

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

# http://exemplo.pt/comer/
RewriteRule ^comer\/$                    index.php?controller=comer&option=index        [QSA,L]

# http://exemplo.pt/comer/camarao/
RewriteRule ^comer\/([a-zA-Z-0-9-_]+)\/  index.php?controller=comer&option=tipo&tipo=$1 [QSA,L]

# http://exemplo.pt/restaurante-do-joao/
RewriteRule ^([a-zA-Z-0-9-_]+)\.html     index.php?controller=anuncio&option=$1         [QSA,L]

The advantage will be that you will not need to make PHP combine the possible routes by combining input and output. You will receive via http://exemplo.pt/dormir/ the ready parameters.

Notice that I alluded to an MVC-based system. Not being your case, then GET represents a physical file that will work with controller . Everything will start with option , so a shallow example would be:

For the input URL index.php , you will have

$_GET['controller'] = 'anuncio'
$_GET['option']     = 'restaurante-do-joao'

In your index.php you do the correct checks - which I have omitted - and send the corresponding file to the controller:

//include anuncio.php
include $_GET['controller'] . 'php';

In the example case, ad.php will be loaded, and you will use http://exemplo.pt/comer/restaurante-do-joao/ to find the company name.

    
09.02.2015 / 07:18