URL friendly with . htaccess

Asked

Viewed 5,625 times

2

I would like some help from you because I have tried a few times but I have not reached the expected result. Need through htaccess, do the following procedure: 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
  • Does that help you? http://blog.thiagobelem.net/ learning Urlsfriendly/

  • Rodrigo, I tried to use Thiago’s methods but I can’t fully apply them. Look at the link: visoart.com ... I need to beautify these links. Have any idea?

  • There must be 3 or 4 answers to this on the site already (I don’t know if any suits you, but there are good examples).

  • @marcosvinicius the best way I know it is the Thiago Belem, it is the simplest and efficient, what is the problem that occurs to you? you read everything he wrote?

5 answers

4

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 a read/orientation file.
Your URL will remain the same and htaccess will interpret it according to the rules and guide access to a particular directory/resource.

Use Rewriterule to write a specific rule for the URL:

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

The example you quoted in the comments below:

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

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 the following URL in the browser:

http://visoart.com/portoalegre
  • How could I rewrite these ridiculous url’s that the browser calls as soon as we trigger the link? vide http://visoart.com

  • @marcosvinicius is exactly the same as I wrote above, see, I changed the answer for you to see the result.

4

Try something like that

RewriteEngine On 

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

RewriteRule ^criacao-sites(.*)$ criacao_de_sites_e_lojas_virtuais.php

3

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

.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 make it easier (instead of the Ifs demonstrated in the example for teaching purposes only) or even query the database to find out which file the URL should be mapped to (which allows you to create a URL translation system!).

I advise using a framework that already encapsulates this for you. A good option is Yii: http://www.yiiframework.com/ .

3

The easiest and most practical way 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 this way. Very good!

2

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

On your website, 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, when the browser calls the url "http://visoart.com/portal/php/criacao-sites", the htaccess rewrite to the correct path in your original file criacao_de_sites_e_lojas_virtuais.php

Browser other questions tagged

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