Rule conflict in . htaccess

Asked

Viewed 213 times

1

I am facing an error with . htaccess file and I am unable to resolve, is the following:

I have the following rule:

RewriteRule ^([^\.]+)$ $1.php [NC,L]

This rule aims to ignore the . php extension of all files accessed in my directory. However, when I use this rule, the others stop working, such as these:

RewriteRule ^([^/]+).$ cidade.php?slug_cidade=$1 [NC,L]
RewriteRule ^restrito/usuarios/pagina/([^/.]+)$ restrito/usuarios.php?pagina=$1 [L,QSA]
RewriteRule ^restrito/edit/usuario/([^/.]+)$    restrito/edit/usuario.php?id=$1 [NC,L]

When I use the rule to ignore extensions, all files: cidade.php, restrito/usuarios.php, and restrito/edit/usuario.php when accessed show Error 404, when I remove the rule to ignore the extensions, they go to work normally, the way I would like.

Some way to solve this problem?

  • Some personal solution?

1 answer

1

It won’t work because at Rewriterule you’re capturing everything ^([^\.]+)$ until the end of the line and replacing the capture group by itself ($1) with the extension ". php", just when you have a file named a.php you will rewrite it like a.php.php, causing the other regex to stop working by containing invalid references.

If you want to ignore the file extension, you should use ^([^\.]+)\..?$ this will capture everything until the last occurrence of "." so you can ignore the extension and do the substitution by ". php" without fear of breaking the path

Browser other questions tagged

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