Rule conflict in .htaccess

1

I'm facing an error with the .htaccess file and I'm not getting resolved, it's 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 extensions, they go to work normally, the way I would like it.

Any way to solve this problem?

    
asked by anonymous 24.08.2017 / 01:48

1 answer

1

It will not work, because in ReWriteRule you are capturing everything ^([^\.]+)$ to the end of the line and replacing the capture group itself ( $1 ) with the extension ".php", so when you have a file of name a.php you will rewrite it as a.php.php, causing the other regex to stop working because they contain 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 replacement for ".php" without fear of breaking the path

    
01.09.2017 / 03:21