.htaccess Internal Server Error

0

I want to make my URLs friendlier and I'm using .htaccess for this.

The problem is that I am returning an Internal Server Error, and I could not identify where my Rule error is.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^autenticar/([a-zA-Z0-9]+)/?& index?authUser=$1

I get ISE upon accessing url: http://localhost/autenticar/teste/ , but I can access http://localhost/index with no problem at all.

What am I doing wrong?

If I remove the line that adds .php at the end of the files, I get "object not found".

If I add [NC, L] , I get another ISE, with this message:

  

"The server encountered an internal error and could not complete its   request. The server is overloaded or there is an error in a   CGI script. "

I'm using Xampp.

    
asked by anonymous 14.02.2018 / 14:03

1 answer

1

By putting the most generic rule first ( RewriteRule (.*) $1.php [L] ) you will get an infinite loop if the file requested by the url does not exist. For example, if you request url http://localhost/autenticar/teste , the way your htaccess is ( RewriteRule (.*) $1.php [L] as the first rule) will be done a redirection to http://localhost/autenticar/teste.php (since the rule takes uri and concantena .php). But if the file ( /autentica/teste.php ) does not exist the rule will be called again, generating a loop. The solution is to put it last. With the fixes the .htacess looks like this (Note the comments #):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# procura pela uri autentica/teste ou autentica/teste/, ou seja com 
# ou sem a barra final ([/]{0,1} caso exista nenhuma ou uma barra)
# a flag QSA adiciona os parametros get, caso exisam, da url original
# para a nova url. Algo como autentica/teste?a=1&b=2 fica 
# index.php?authUser=teste&a=1&b=2
RewriteRule ^autenticar/([a-zA-Z0-9]+)[/]{0,1} index.php?authUser=$1 [QSA,L]

# para evitar que a próxima regra seja executada caso exista algum match na
# primeira
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

Using:

  • http://localhost/autenticar/teste/ gets http://localhost/index.php?authUser=teste
  • http://localhost/autenticar/teste/?a=1&b=2 gets http://localhost/index.php?authUser=teste&a=1&b=2
  • http://localhost/autenticar/teste?a=1&b=2 gets http://localhost/index.php?authUser=teste&a=1&b=2
  • http://localhost/pagina1 is http://localhost/pagina1.php , but if page1.php does not exist the internal server error will be displayed, because it will generate a loop (as the file does not exist a new redirect will be done).

Recommended reading:

14.02.2018 / 18:43