.htaccess does not work the rule

Asked

Viewed 178 times

0

I could get my urls like this?

produtos/nome-categoria
ptodutos/nome-categoria/nome-subcategorias

'Cause I can’t do it, look how I’m making the rule in . htaccess

<IfModule mod_rewrite.c>

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

    #fix rules
    RewriteRule ^pagina-inicial/?$ index.php [NC,L]       
   RewriteRule ^produtos/(.*)/(.*)$ categorias.php?id_categoria=$1&id_subcategoria=$2 [NC,L]
</IfModule>

thus only ptoducts/name-category/name-subcategories Funcin

1 answer

0

You have to make a part of regex optional.

For that, we use a (?:group) with the quantifier ?.

^produtos/([^/]*)(?:/(.*))?
#                ^^^^^^^^^^
#                 opcional


.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^pagina-inicial/?$ index.php [NC,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^produtos/([^/]*)(?:/(.*))? categorias.php?id_categoria=$1&id_subcategoria=$2 [NC,L,QSA]
</IfModule>


Test in:

Or watch it work:

  • to access this product/name-category worked plus when access like this product/name-category/sub-category does not work códigoRewriterule products/([^/])(?:/(.))/([^/])(?:/(.))? subcategories.php? id_categoria=$1&id_subcategory=$2 [NC,L,QSA]código

  • @Wagner You can see the examples I added.

Browser other questions tagged

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