URL friendly with .htaccess

1

I would like some help from you because I tried a few times but I did not get the expected result. I need through htaccess, to do the following: change the url that is shown in the browser, example:

Current URL:

meusite.com.br/criacao_de_sites_e_lojas_virtuais.php

New URL:

meusite.com.br/criacao-sites
    
asked by anonymous 16.05.2014 / 18:53

5 answers

3

Try something like this

RewriteEngine On 

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

RewriteRule ^criacao-sites(.*)$ criacao_de_sites_e_lojas_virtuais.php
    
16.05.2014 / 20:40
3

htaccess will not change your URL, it will remain the same.
It is the opposite of what you wrote, the user will write:

meusite.com.br/criacao-sites

htaccess is an orientation / reading file. Your URL will remain the same and htaccess will interpret it according to the rules and guide access to a certain directory / resource.

Use RewriteRule to write a rule specific to the URL:

RewriteEngine On
RewriteBase /
RewriteRule ^criacao-sites criacao_de_sites_e_lojas_virtuais.php [NC,QSA,L]

The example you cited in the comments below:

http://visoart.com/portal/php/visoart_empresa_web_design_design_grafico_porto_alegre.php

It would look like this:

#ativamos a reescrita
RewriteEngine On
RewriteBase /

#quando o utilizador escrever no browser .../portoalegre, o htaccess executa um redireccionamento interno para o arquivo "visoart_empresa_web_design_design_grafico_porto_alegre.php" dentro do directório "/portal/php/".
RewriteRule ^portoalegre portal/php/visoart_empresa_web_design_design_grafico_porto_alegre.php [NC,QSA,L]

With the above change, just write in the browser the following URL:

http://visoart.com/portoalegre
    
16.05.2014 / 19:15
2

Redirect all requests to an input script for your application. In this input script (index.php in the example below) interpret the URL including the appropriate page (this allows you to do access control and errors of your application).

.htaccess

RewriteEngine on

# Nao aplica o redirecionamento caso 
# o que esteja sendo acessado seja um arquivo ou pasta.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Redireciona para o arquivo index.php
RewriteRule . index.php

index.php

// Remove da URL a pasta da aplicacao,
// deixando apenas os parametros.
$aux = str_replace('index.php', '', $_SERVER['SCRIPT_NAME']);
$parameters = str_replace($aux, '', $_SERVER['REQUEST_URI']);

// Recupera as partes da URL.
// Se você acessar http://meusite.com.br/criacao-sites
// $urlParts será:
//      array('criacao-sites')
//
// Se você acessar http://meusite.com.br/criacao-sites/blogs
// $urlParts será:
//      array('criacao-sites', 'blogs')
$urlParts = explode('/', $parameters);

// Com base nas partes redirecione como desejar.
if ($urlParts[0]=='criacao-sites')
    require_once 'criacao-sites.php';
else if ($urlParts[0]=='contato')
    require_once 'contato.php';
else
    require_once 'erro-404.php';

You can create a mapping array to facilitate (instead of the IFs shown in the example just for didactic effect) or even query the database to know what file the URL should be mapped to (which allows you to create a URL translation system!).

I recommend using a framework that already encapsulates this for you. A good option is Yii: link .

    
17.05.2014 / 03:01
2

The easiest and most practical is this:

<IfModule mod_rewrite.c>
       RewriteEngine On

        RewriteRule ^criacao-sites/?$ /criacao_de_sites_e_lojas_virtuais.php [NC,L]
        # Você pode adicionar quantas URLs quizer. Ex:
        RewriteRule ^nova-url-amigavel/?$ /nome-do-arquivo-que-ira-abir.php [NC,L]

    </IfModule>

I only use it that way. Very good!

    
17.05.2014 / 04:46
1

I do not know you understood well how friendly url works, so I'll try to explain. htaccess will not rewrite your url in the browser from criacao-sites criacao_de_sites_e_lojas_virtuais.php to criacao-sites , what you need to do is, rewrite your html code, see:

On your site, where the code is:

<a href="criacao_de_sites_e_lojas_virtuais.php" title="Criação de sites">Criação</a>

Change to:

<a href="criacao-sites" title="Criação de sites">Criação</a>

Then use the solution proposed by @Filipe in your htaccess , for when the browser calls the url " link ", htaccess rewrite to the correct path in its original file criacao_de_sites_e_lojas_virtuais.php

    
16.05.2014 / 20:49