Error to 404 when setting Friendly URL in . htaccess

Asked

Viewed 185 times

1

Is giving 404 error while accessing the Friendly URL I tried to do by .htaccess: RewriteRule ^es/guarapari/([0-9]+)/([[a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC]

.full htaccess:

<IfModule mod_rewrite.c>

  RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

  RewriteRule ^es/guarapari/([0-9]+)/([[a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC]  

  RewriteRule ^es/guarapari\/?$ cidade.php?cidade=100 [L]

</IfModule>

Access usually the address: https://localhost/guiacomercial/es/guarapari/

Now when I try to access: https://localhost/guiacomercial/es/guarapari/empresa/1/acabamento generates error 404.

1 answer

1


Has a mistake:

RewriteRule ^es/guarapari/([0-9]+)/([[a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC]
#                                   ^
#                                  aqui

Should be

RewriteRule ^es/guarapari/([0-9]+)/([a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC]


However, this rule does not match a URL with /empresa as

https://localhost/guiacomercial/es/guarapari/empresa/1/acabamento
                                            ^^^^^^^^

If you want to accept /empresa optionally:

RewriteRule ^es/guarapari/(?:empresa/)?([0-9]+)/([a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC]


.htaccess:

<IfModule mod_rewrite.c>

  RewriteEngine On

  # https://localhost/guiacomercial/es/guarapari/empresa/1/acabamento
  # https://localhost/guiacomercial/es/guarapari/1/acabamento
  #  -> /guiacomercial/empresas.php?id=1&nome=acabamento
  RewriteRule ^es/guarapari/(?:empresa/)?(\d+)/([a-z0-9-]+)/?$ empresas.php?id=$1&nome=$2 [NC,QSA,L]

  # https://localhost/guiacomercial/es/guarapari
  # https://localhost/guiacomercial/es/guarapari/
  #  -> /guiacomercial/cidade.php?cidade=100
  RewriteRule ^es/guarapari/?$ cidade.php?cidade=100 [NC,QSA,L]

</IfModule>

Browser other questions tagged

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