How to use friendly Urls?

Asked

Viewed 859 times

3

I currently use the MVC in my project and when I access my system it shows a url like this:

http://localhost/view/html/cliente.html

what I would like to get is:

localhost/cliente because it’s the only file called client that exists, if you have a new folder like this:

localhost/boleto/bradesco

and would be accessed by:

http://localhost/view/html/boleto/bradesco.html

Currently I use jQuery,php,html, each separate from the other. there is some solution to create friendly urls dynamically using these languages to get this result?

  • 1

    Using the example of .htaccess from @Leocbs, I believe that modify the last line (RewriteRule ^(.*)$ index.php?pag=$1) for RewriteRule ^(.*)$ view/html/$1.html solves your problem.

1 answer

3


Hello, you use the . htaccess file to dynamically handle your urls.

Below a friendly url scenario:

.htaccess

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

What this code does is manipulate your URL (for it to work it requires the Modrewrite function to be active on your server). From now on, your website’s Urls can be written more simply. Internally apache will interpret an address that looks like this: seusite.com.br/noticia/12; as if it were like this: seusite.com.br/index.php? pag=noticia/12;

Now let’s create the file that will manipulate the URL, this file will access the variable pag. Let’s call this file url.php

< ? php
//a variavel atual, vai receber o que estiver na variável pag
//se não tiver nada, ela recebe o valor: principal“”
$atual = (isset($_GET['isw'])) ? $_GET['isw'] : 'principal';

//aqui setamos um diretório onde ficarão as páginas internas do site
$pasta = 'paginas';

//vamos testar se a variável pag possui alguma “/”
//ou seja, caso a url seja: /noticia/2
if (substr_count($atual, '/') > 0) {
//utilizamos o explode para separar os valores depois de cada “/”
$atual = explode('/', $atual);
/*testamos se depois do endereço do site, o valor da página é um arquivo existente
caso não exista, iremos atribuir o valor “erro” que será uma página de erro
personalizada que existirá dentro da pasta '$pasta', esse arquivo será incluido sempre que um endereço invalido for digitado */
$pagina = (file_exists("{$pasta}/" . $atual[0] . '.php')) ? $atual[0] : 'erro';
//ao que tiver depois da segunda “/” atribuiremos a variavel $id
$id = $atual[1];
//ao que tiver depois da terceira “/” atribuiremos a variavel $busca
$busca = @$atual[2];    
} else {
$pagina = (file_exists("{$pasta}/" . $atual . '.php')) ? $atual : 'erro';
$id = 0;
$frame=0;
}

//com o uso de URL amigáveis se torna necessário que arquivos sejam chamados
//com o seu caminho completo, isso porque as imagens levam em consideração a URL
// ex: <img src=”<?=$siteUrl?>/pasta/arquivo.png” />
$siteUrl = "http://seusite.com.br”;

?>

Directory tree of our website:

www/.htaccess
www/index.php
www/url.php
www/paginas/principal.php
www/paginas/{TODAS AS OUTRAS PAGINAS ITERNAS}
www/imagens

Source

  • enabled rewrite mod restarted apache, created the .htaccess put Cod, then created the url.php and added all this code, are at www/. htaccess ,index.html,url.php,view,model,controller

  • worked the urls?

  • didn’t work, I should add the.php url to run somewhere ? in case I’m not working with the abstraction of php I’m only with the html that is in the view/html/page.html folder

Browser other questions tagged

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