.htaccess Internal Server Error

Asked

Viewed 654 times

0

I want to make my Urls more friendly and am using the .htaccess for that reason.

The problem is that it is returning me 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 when accessing the url: http://localhost/autenticar/teste/, but I can access http://localhost/index 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 receive 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 the Xampp.

1 answer

1


By placing 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 the url http://localhost/autenticar/teste, the way your htaccess is (RewriteRule (.*) $1.php [L] as the first rule) a redirect will be made to http://localhost/autenticar/teste.php (since the rule takes Uri and concantena . php). But if the file (/autentica/teste.php) not existing the rule will be called again, generating a loop. The solution is to put it last. With the corrections o . 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/ stays http://localhost/index.php?authUser=teste
  • http://localhost/autenticar/teste/?a=1&b=2 stays http://localhost/index.php?authUser=teste&a=1&b=2
  • http://localhost/autenticar/teste?a=1&b=2 stays http://localhost/index.php?authUser=teste&a=1&b=2
  • http://localhost/pagina1 stays http://localhost/pagina1.php, but if pagina1.php does not exist the error of Internal server will be displayed as it will generate a loop (as the file does not exist a new redirect will be made).

Recommended reading:

Browser other questions tagged

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