php – Friendly URL, how to make it work with HTACCESS

Question:

I created some rules in my .htaccess file to format the URL view, but I'm having difficulty passing the parameter to the page showing the products and showing the formatted Friendly URL , the code looks like this:

RewriteEngine On
RewriteRule ^Pagina-Inicial\/?$ index.php?acessando=home [L]
RewriteRule ^institucional\/?$ index.php?acessando=institucional [L]
RewriteRule ^promocoes\/?$ index.php?acessando=promocoes [L]
RewriteRule ^lojas\/?$ index.php?acessando=lojas [L]
RewriteRule ^lojas\/?$ index.php?acessando=loja [L]
RewriteRule ^contato\/?$ index.php?acessando=contato [L]
RewriteRule ^trabalhe\/?$ index.php?acessando=trabalhe [L]

RewriteRule ^([a-z,0-9,A-Z,_-]+)$ /produtos.php?dep=$1
RewriteRule ^([a-z,0-9,A-Z,_-]+)\/([a-z,0-9,A-Z,_-]+)$ /produtos.php?dep=$1&sup=$2
RewriteRule ^([a-z,0-9,A-Z,_-]+)\/([a-z,0-9,A-Z,_-]+)\/([a-z,0-9,A-Z,_-]+)$ /detalhes.php?produto=$1&dep=$2&sub=$3

For example, when the customer clicks on KITCHEN he redirects to the products.php page passing the parameter and showing something like this in the URL , I made an edit to try to make it clearer.

When the user clicks on the main menu, today it looks like this:

http://moveissaobento.com.br/msb/produtos.php?dep=1
http://moveissaobento.com.br/msb/produtos.php?dep=2
http://moveissaobento.com.br/msb/produtos.php?dep=3

I would like to leave it like this:

http://moveissaobento.com.br/msb/COZINHA
http://moveissaobento.com.br/msb/DORMITORIO
http://moveissaobento.com.br/msb/SALA-DE-JANTAR

When the user clicks on the submenu, today it looks like this:

http://moveissaobento.com.br/msb/produtos.php?dep=1&sub=4
http://moveissaobento.com.br/msb/produtos.php?dep=2&sub=15
http://moveissaobento.com.br/msb/produtos.php?dep=3&sub=34

I would like to leave it like this:

http://moveissaobento.com.br/msb/COZINHA/AMBIENTE-MESA-C/-BANQUETAS
http://moveissaobento.com.br/msb/COZINHA/AMBIENTE-MESA-C/-CADEIRAS

I don't know exactly how to show the URL in user-friendly form.

Answer:

The QSA flag informs that only QUERY_STRING can access them.
The NC (nocase) flag is to ignore case sensitive (upper/lower case).
The L (last) flag is a flag to indicate that the current rule should be applied immediately, regardless of other rules (ie, it becomes independent).

The R (redirect) flag causes an HTTP redirect (when there is the domain or server written in the browser's URL).

To start a rule use: ^ , to end the rule use: $ , use regular expressions to handle the data that will be provided in the browser URL to interpret the output of the system url.

For each set of rules, example (regra1)(regra2)(regran...) in front of the rule put the system address: sua_pagina.php?data1=$1&data2=$2&data3=$... , in the system link , put the formatted rule, it will read the page, and after the URL, put the flags listed inside square brackets [] , there are several flags, which can be checked here .

This allows Google or other search engines to better index your site.
To learn more, visit the Apache documentation .

 RewriteEngine On
    #aqui criamos uma condição para que os arquivos sejam ignorados nas regras abaixo
    RewriteCond %{REQUEST_FILENAME} !-f
  #aqui criamos uma condição para que diretórios sejam ignorados nas regras abaixo
    RewriteCond %{REQUEST_FILENAME} !-d
    #aqui definimos onde começa a base das regras
    #vamos supor que fosse um subdiretório, como por exemplo: /vs1/
    RewriteBase /
    #aqui ignoramos o uso de barra no final
    RewriteRule ^(.+)\/?$ $1 [R,L]

    #fix rules 
    RewriteRule ^pagina-inicial$ index.php?acessando=home [QSA,L,NC]
    RewriteRule ^institucional$ index.php?acessando=institucional [QSA,L,NC]
    RewriteRule ^promocoes$ index.php?acessando=promocoes [QSA,L,NC]
    RewriteRule ^lojas$ index.php?acessando=lojas [QSA,L,NC]
    RewriteRule ^loja$ index.php?acessando=loja [QSA,L,NC]
    RewriteRule ^contato$ index.php?acessando=contato [QSA,L,NC]
    RewriteRule ^trabalhe$ index.php?acessando=trabalhe [QSA,L,NC]

    #dinamic rules
    RewriteRule ^msb\/([0-9A-z_-]+)$ msb/produtos.php?dep=$1 [QSA,L,NC]
    RewriteRule ^msb\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ msb/produtos.php?dep=$1&produto=$1 [QSA,L,NC]
    RewriteRule ^msb\/([0-9A-z_-]+)\/([0-9A-z_-]+)\/([0-9A-z_-]+)$ msb/detalhes.php?produto=$1&dep=$2&sub=$3 [QSA,L,NC]

Here's a suggested site for you to customize your rules: http://regexr.com/

Scroll to Top