Dynamic url with php

Asked

Viewed 343 times

1

I have this url on my website

categorias.php?id_categoria=1

I want to make a rule to stay

produtos/nome-categoria

Since the category name I’m already pulling from the bank with php

my . htacsess is so with a rule already made for the home page

<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]

    </IfModule>

2 answers

1

Add this rule:

RewriteRule ^produtos/(.*)$ /categorias.php?id_categoria=$1 [NC,L]

And to make the call, the url would be:

produtos/categoria

Edited:

In your case it would be:

RewriteRule ^produtos/(.*)$ categorias.php?id_categoria=$1 [NC,L]
  • According to his code, I could understand that he goes for the ID category, which must be an integer number. You must adapt the code if you want to search by category name.

  • returned Error 404. I changed php to fetch skin Name. products/name-category

  • Error 404 means the url was not found. Try to check if the path is correct.

  • You may have to restart the Apache service

  • do i access the address like this categories.php? id_categoria=1

  • Then it would look like this: produtos/1

  • when I place Rewriterule products/(.*)$ /categories.php? id_categoria=$1 [NC,L] returns ero 404

  • products/1 tbm returned the 404

  • face soh one more thing if I have categories.php? id_category=1&name_category=name-category and want to access it like this categories/name-category ?

  • Wagner, I think you should create another topic, because it already escapes from the original question and you may have negative points so.

Show 6 more comments

-1

I’m doing so, with the category works more as I put the subcategory too?

produtos/nome-categoria/nome-subcategoria


<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 [NC,L]
   RewriteRule ^produtos/(.*)$ categorias.php?id_categoria=$1&nome_categoria=$2 [NC,L]
 RewriteRule ^produtos/(.*)$ subcategorias.php?id_subcategoria=$1&nome_categoria=$2 [NC,L]
        </IfModule>

Browser other questions tagged

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