Carlos Rocha Posted June 20, 2018 at 04:00 PM Report #611057 Posted June 20, 2018 at 04:00 PM Tenho o arquivo .htaccess abaixo: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /crud/ #acessa as requisições abaixo #se for um arquivos vazio RewriteCond %{REQUEST_FILENAME} -s [OR] #ou se for um link de referência (simbólico) RewriteCond %{REQUEST_FILENAME} -l [OR] #ou se for um diretório RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^(.) index.php </IfModule> Na minha classe de Constantes (.php), tenho uma constante: static private $livre = false; E no construtor da classe eu faço a verificação da condição de $livre public function __construct($_util) { if(self::$livre == false) { header("Location: ".$_SERVER["SERVER_PATH"]."/crud/manutencao.php"); exit; } Acontece que quando $livre = true; funciona bem; Porém, quando: $livre = false; não consigo chegar ao arquivo de manutenção como está na classe. Isto é, dá erro de muitas tentativas: Esta página não está funcionando Redirecionamento em excesso por localhost Tente limpar os cookies. ERR_TOO_MANY_REDIRECTS Acho que preciso fazer uma tratativa no arquivo .htaccess para validar a condição de $livre também. Algo como: if ($livre == false) manutenção.php else index.php Mas não sei como fazer isso uma vez que existe no .htaccess a regra dizendo que todas requisições chegarem no index.php;
Rui Carlos Posted June 20, 2018 at 07:29 PM Report #611059 Posted June 20, 2018 at 07:29 PM Com a pouca informação que disponibilizas, diria que no .htaccess estás a redireccionar para o index.php, que por sua vez redirecciona para /crud/manutencao.php, que volta a ser redireccionado para o index.php, entrando em ciclo infinito. Deves adicionar mais uma excepção aos casos em que não queres que o .htaccess redireccione (diria que adicionar o caso RewriteCond %{REQUEST_FILENAME} -f era boa ideia). Rui Carlos Gonçalves
Carlos Rocha Posted June 20, 2018 at 09:16 PM Author Report #611060 Posted June 20, 2018 at 09:16 PM (edited) sim, mas se o arquivo é manutencao.php e está no mesmo diretório que o index.php, como ficaria meu novo .htaccess incluindo a regra RewriteCond %{REQUEST_FILENAME} -f ? pois fiz uma tentativa aqui e deu erro 500; E como encerrar com o ciclo infinito? Tentei da forma abaixo mas não funcionou <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /crud/ #acessa as requisições abaixo #se for um arquivos vazio RewriteCond %{REQUEST_FILENAME} -s [OR] #ou se for um link de referência (simbólico) RewriteCond %{REQUEST_FILENAME} -l [OR] #ou se for um diretório #RewriteCond %{REQUEST_FILENAME} == manutencao.php #RewriteRule ^(.*) manutencao.php [R=301,L] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] RewriteRule ^(.*) index.php ErrorDocument 400 http://localhost/crud/index.php ErrorDocument 401 http://localhost/crud/401.php ErrorDocument 403 http://localhost/crud/403.php ErrorDocument 404 http://localhost/crud/404.php ErrorDocument 500 http://localhost/crud/500.php </IfModule> No caso localhost no teste local. Dá o erro 500 Edited June 20, 2018 at 09:19 PM by carcleo
Carlos Rocha Posted June 21, 2018 at 05:14 PM Author Report #611067 Posted June 21, 2018 at 05:14 PM RewriteEngine On RewriteBase /crud/ # TODAS URLS CAEM NO INDEX. RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^/]*)$ index.php [L] # SE POR ACASO, A URL FOR manutencao.php, LIBERA ACESSO A ELA # NESSE CASO, FICA ÓBVIO QUE PARA TODAS AS PÁGINA PRECISARÁ HAVER UM REDIRECIONAMENTO # À manutenção.php. SENÃO, NÃO FUNCIONARÁ! RewriteCond %{HTTP_HOST} ^manutencao.php [NC] RewriteRule ^(.*)$ manutencao.php/$1 [L,R=301] # ROTAS DE ERROS ErrorDocument 401 http://localhost/crud/401.php ErrorDocument 403 http://localhost/crud/403.php ErrorDocument 404 http://localhost/crud/404.php ErrorDocument 500 http://localhost/crud/500.php
Rui Carlos Posted June 21, 2018 at 05:51 PM Report #611069 Posted June 21, 2018 at 05:51 PM Isto RewriteCond %{SCRIPT_FILENAME} !-f (equivalente a RewriteCond %{REQUEST_FILENAME} !-f), é o que lhe fazia falta, para evitar o redireccionamento caso exista um ficheiro com o nome solicitado. O comentário do bloco está errado (# TODAS URLS CAEM NO INDEX.), pois só são redireccionados URLs para os quais não existe ficheiro nem directório com o nome indicado. Tenho dúvidas que o bloco seguinte (linha RewriteCond %{HTTP_HOST} ^manutencao.php [NC] e seguinte) sirva de alguma coisa, visto que compara o domínio com o nome de um ficheiro. Rui Carlos Gonçalves
Carlos Rocha Posted June 21, 2018 at 09:58 PM Author Report #611075 Posted June 21, 2018 at 09:58 PM Oh Ruy, segundo o seu entendimento, como ficaria? Pois realmente, ifz os testes aqui e NÃO funcionou como esperado!
Rui Carlos Posted June 22, 2018 at 07:09 PM Report #611086 Posted June 22, 2018 at 07:09 PM Esperaria que isto fosse suficiente: RewriteEngine On RewriteBase /crud/ RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule . index.php [L] Rui Carlos Gonçalves
Carlos Rocha Posted June 22, 2018 at 07:24 PM Author Report #611088 Posted June 22, 2018 at 07:24 PM (edited) Bom, acho que tive progresso: Tenho o .htaccess abaixo e parece estar funcionando bem: <ifModule mod_rewrite.c> # LIGA O MOTOR DE REESCRITA (Rewrite) RewriteEngine on # BUSCA PELA BASE /teste/ NO HOST ACESSADO RewriteBase /teste/ # FAZ UMA ESCESSÃO DE REDIRECIONAMENTO PARA A PÁGINA manutencao.php CASO O NAVEGADOR RECEBA ELA NA URL RewriteCond %{REQUEST_URI} !/manutencao.php$ [NC] # FAZ UMA ESCESSÃO E LIBERA IMAGENS NESTA PAGINA RewriteCond %{REQUEST_URI} !.(jpe?g?|png|gif) [NC] # REENVIA QUALQUER ACESSO AO SITE PARA A PÁGINA index.php NO HOST INDICADO LIBERANDO AS EXCESSÕES ACIMA DESCRITAS RewriteRule .* index.php #RewriteRule .* http://localhost/teste/index.php #RewriteRule .* http://localhost/teste/index.php [R=302,L] </ifModule> Porém, tenho algumas dúvidas: A ) Nenhuma das linhas abaixo funciona, ambas dá erro 500: #RewriteRule .* http://localhost/teste/index.php #RewriteRule .* http://localhost/teste/index.php [R=302,L] Ou seja, não consigo enviar para o endereço de host completo apenas redireciona corretamente para a página específica :index.php Neste caso, se eu acessar por exemplo: http://localhost/teste/outroteste/testando/index.php o redirecionamento se fará para http://localhost/teste/outroteste/testando/index.php ou seja, para o mesmo link e não para http://localhost/teste/index.php Como se era esperado. Como corrigir isso? B) A correção do item anterior deve resolver o problema deste item que é a lincagem dos arquivos css, js, imagens e tudo mais que compõem o arquivo a ser aberto. Quem poderá me ajudar? Arquivos envolvidos: .htaccess <ifModule mod_rewrite.c> # LIGA O MOTOR DE REESCRITA (Rewrite) RewriteEngine on # BUSCA PELA BASE /teste/ NO HOST ACESSADO RewriteBase /teste/ # FAZ UMA ESCESSÃO DE REDIRECIONAMENTO PARA A PÁGINA manutencao.php CASO O NAVEGADOR RECEBA ELA NA URL RewriteCond %{REQUEST_URI} !/manutencao.php$ [NC] # FAZ UMA ESCESSÃO E LIBERA IMAGENS NESTA PAGINA RewriteCond %{REQUEST_URI} !.(jpe?g?|png|gif) [NC] # REENVIA QUALQUER ACESSO AO SITE PARA A PÁGINA index.php NO HOST INDICADO LIBERANDO AS EXCESSÕES ACIMA DESCRITAS RewriteRule .* index.php #RewriteRule .* http://localhost/teste/index.php #RewriteRule .* http://localhost/teste/index.php [R=302,L] </ifModule> index.php INDEX <?php header ("Location: http://localhost/teste/manutencao.php"); ?> manutencao.php <?php echo "MANUTENÇÃO "; ?> Edited June 23, 2018 at 12:44 PM by carcleo
Carlos Rocha Posted June 24, 2018 at 05:28 PM Author Report #611102 Posted June 24, 2018 at 05:28 PM <ifModule mod_rewrite.c> # LIGA O MOTOR DE REESCRITA (Rewrite) RewriteEngine on # BUSCA PELA BASE /crud/ NO HOST ACESSADO RewriteBase /crud/ # FAZ UMA ESCESSÃO DE REDIRECIONAMENTO PARA A PÁGINA manutencao.php CASO O NAVEGADOR RECEBA ELA NA URL # PERCEBA O ! ANTES DO NOME DO ARQUIVO. ISSO DIZ AO .htaccess QUE O ARQUIVO EM QUESTÃO NÃO SEGUE A REGRA E ABRE NORMALMENTE RewriteCond %{REQUEST_URI} !/manutencao.php$ [NC] # FAZ UMA ESCESSÃO E LIBERA IMAGENS NESTA PAGINA # NOTE QUE PAR ESAS EXTENSÕES, O REDIRECIONAMENTO NÃO ACONTECE E DÁ ERRO 404 CASO A IMAGEM NÃO EXISTA # AS DEMAIS EXTENSÕES SÃO TODAS REENVIADAS PARA A INDEX RewriteCond %{REQUEST_URI} !.(jpe?g?|png|gif|css|js) [NC] # REENVIA QUALQUER ACESSO AO SITE PARA A PÁGINA index.php NO HOST INDICADO LIBERANDO AS EXCESSÕES ACIMA DESCRITAS RewriteRule .* index.php </ifModule>
toarelli Posted July 14, 2018 at 11:30 PM Report #611316 Posted July 14, 2018 at 11:30 PM Caro amigo Carcleo eu uso assim e não tive problemas. RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
Carlos Rocha Posted July 15, 2018 at 11:39 AM Author Report #611321 Posted July 15, 2018 at 11:39 AM (edited) @torelli. Mas aí você não faz menção à exceção do arquivo de manutenção. Ou seja, se ele for chamado, o sistema entrará em um loop infinito. Edited July 15, 2018 at 11:59 AM by carcleo
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now