Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
Apache .htaccess mapping non-existent file to a script

This is .htaccess file I currently have:

Options -Indexes
AddDefaultCharset utf-8
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
<Files ".ht*">
    Require all granted
</Files>
RewriteEngine on
RewriteRule ^error\.html$ / [R=301]
RewriteRule ^\.ht / [L,R=301,NC]

I need to add a specific rule to map a non-existent file to a script, like so:

RewriteRule /folder/file.txt /folder/file.php [L]

How do I add this rule to the .htaccess?

I tried:

Options -Indexes
AddDefaultCharset utf-8
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
<Files ".ht*">
    Require all granted
</Files>
RewriteEngine on

RewriteCond "%{REDIRECT_URL}" "/folder/file.txt"
RewriteRule ^error\.html$ /folder/file.php [L]

RewriteRule ^error\.html$ / [R=301]
RewriteRule ^\.ht / [L,R=301,NC]

but it doesn't work.

Thank you!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

With your shown samples, please try following htaccess Rules file. Make sure to keep your htaccess file in root directory.

Also make sure to clear your browser cache before testing your URLs.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/folder/file\.php -f [NC]
RewriteRule ^folder/file\.txt$ /folder/file.php [NC,L]


OR in case you want to match any .txt in uri not only file.txt then try following Rules, make sure to try either above rules set OR following one at a time only.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/folder/$1\.php -f [NC]
RewriteRule ^folder/([^.]*)\.txt$ /folder/$1.php [NC,L]
over 4 years ago · Santiago Trujillo Relatório

0

To map a non-existent file /folder/file.txt to /folder/file.php you can use the following :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder/file\.txt$ /folder/file.php [L]

Here is a generic rule that rewrites all .txt files to .php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.tx$ /$1.php [L]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda