URL friendly, how to work with HTACCESS

22

I have created some rules in my .htaccess file to format the URL view, but I am having difficulty passing the parameter to the page that shows the products and show formatted, 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 client clicks on KIT redirects to the products.php page by passing the parameter and showing in URL something like that, I made an edit to try to leave more Of course.

When the user clicks the main menu, 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 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 the submenu, 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 this:

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

I'm not exactly sure how to show the URL in the friendly way.

    
asked by anonymous 11.03.2015 / 21:19

2 answers

22

The QSA flag says that only QUERY_STRING will be able to access them. The NC (nocase) flag is to ignore case sensitive (high / low). The L (last) flag is a flag indicating that the current rule should be applied immediately, without regard to other rules (ie, it becomes independent).

The R (redirect) flag causes an HTTP redirect (when the domain or server is typed in the browser 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 rule set, 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, place the formatted rule, which it will read the page, and after the URL, place the flags listed inside brackets [] , there are several flags, which can be checked here .

This allows Google or other search engines to index your site better. To learn more, visit 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 is a website suggestion for you to customize your rules:      link

    
14.09.2015 / 23:01
6

I do not find it very interesting to write your rules like this

RewriteRule ^Pagina-Inicial\/?$ index.php?acessando=home

Looking at the side of a small system does not seem to be a problem but if you have a very large system there will be a need for you to write many rules, then it is not interesting, but if you receive the complete url query you can

# compressão básica
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js)$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# Proteger os arquivos e diretórios
<FilesMatch "(\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)? |xtmpl)|code-style\.pl|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
</FilesMatch>
# Não mostrar listagens de diretório
Options -Indexes
# Regras básicas de reescrita, parar desnecessários bot PERL, bloquear diretórios de subversão
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Acesso proibido"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
RewriteRule ^([A-Za-z0-9\/]+)$ index.php?$1 [NC,L]
</IfModule>

For example, if the request is " link " my rule:

RewriteRule ^([A-Za-z0-9\/]+)$ index.php?$1 [NC,L]

will receive "news / economy" because my regex allows letters, numbers and the bar, so I can treat my urls in the code and in case any user type and does not have on my system I already redirect to a 404 page. Hope this helps. ;)

    
16.09.2015 / 15:42