URL friendly, how to make it work with HTACCESS

Asked

Viewed 32,776 times

24

I created some rules in my file .htaccess to format the display of URL, but I’m having difficulty passing the parameter to the page that shows the products and show the URL Friendly formatted, the code is 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 KITCHEN redirects to the page php products. passing the parameter and showing in URL something like this, I made an edit to try to make it clear.

When the user clicks on the main menu, today it is 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’d like to leave it at that:

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’d like to leave it at that:

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 URL in the friendly way.

  • In your PHP you already have some code to get the item ID (category, subcategory and product) through the name passed?

  • 1

    Hello @Andréribeiro, I have the code that receives the department id, subdepartamento and product but not as you said, by the name passed.

  • Another question: do you really need to pass the department id and subdepartment to open the product page? Because if you really need it you’ll have to pass the id of both in the URL somehow.

  • Yes @Andréribeiro, I need to pass these id s, the way it is and what I read in some articles have as, but I could not.

  • this bar will give conflict in the rule: ENVIRONMENT-MESA-C/-CHAIRS, you have to treat this for: ENVIRONMENT-TABLE-WITH-CHAIRS.

  • @Kaduamaral, thanks for the excellent link, helped me a lot.

Show 2 more comments

2 answers

26


The flag QSA, informs that only QUERY_STRING you can access them.
The flag NC (nocase), is to ignore case sensitive (high/low box).
The flag L (last) is a flag to indicate that the current rule must be applied immediately, without considering other rules (i.e., it becomes independent).

The flag R (redirect) causes an HTTP redirect (when the domain or server is written to the browser URL).

To start a rule use: ^ , to end the rule use: $, use regular expressions to process 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=$..., on the system link, put the formatted rule, which it will read the page, and after the URL, put the listed flags inside square brackets [], there are several flags, which may be conferred here.

This allows the Google or other search engines better index your site.
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 suggested website for you to customize your rules: http://regexr.com/

  • 1

    Can you explain why? It benefits those who do not know much about it. More than the author of the question, the idea is that the answers help future visitors with similar or equal questions. Thanks!

  • 2

    @brasofilo, I gave a small explanation, I hope to have helped.

  • 1

    I’ve certainly learned a couple of things from your explanation. Thank you!

5

I don’t find it very interesting to write your rules like that

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 the need for you to write many rules, then it is no longer interesting, but if you receive the full url query you can treat it in PHP and direct it the way you want, an example of htaccess that I use is this

# 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 so "http://example.com/news/economy" 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 if any that the user type and not have in my system I already redirect to a page 404. hope I’ve helped. ;)

  • Bingo ! Perfect this regex that allows the bar !

  • And if I need to add the hyphen to that expression?

  • 1

    Denis can use [A-Za-z0-9\-\/], I believe it would look something like this, anything you can test regex on the site regexr.com and make one the way you think best, even using "wildcards" to accept any character.

Browser other questions tagged

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