Redirect file access via htacess

Asked

Viewed 346 times

0

Good when user type some file with the .txt I have to send it to an authentication file called download.php

Example, if user type www.meusite.com.br/documento.txt I have to redirect him to the archive download.php.

I’m trying to do it like this:

RewriteRule ^(.+)\.txt$ Download.php

But instead of redirecting it is opening the txt file in the browser normally.

Someone knows how to do it?

________________ EDIT ___________________ Like this my htacess

# Configurações do url
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Redirecionamento do Search
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) Site/$1 [QSA,L]

    # A menos diretório, remover barra final
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/$ http://127.0.0.1/Site/$1 [R=301,L]

    # Resolver .php para urls externos php
    RewriteRule ^([^/.]+)$ $1.php [L]

    # AQUI NÃO ESTA FUNCIONANDO
    RewriteRule ^(.*)\.txt$ download.php?arquivo=$1

    # Tratamento dos erros
    ErrorDocument 500  http://127.0.0.1/erro?n=500
    ErrorDocument 403  http://127.0.0.1/erro?n=403
    ErrorDocument 404  http://127.0.0.1/erro
</IfModule>

# Evitar a listagem de diretórios
Options -Indexes

# Protege o arquivo .htaccess
<files ~ "^.*\.([Hh][Tt][Aa])">
    order allow,deny
    deny from all
    satisfy all
</files>

# Forçar o uso da codificação UTF-8
<FilesMatch ".(htm|html|php|css|js)$">
    AddDefaultCharset UTF-8
</FilesMatch>

# Compressão básica
<IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|php|js)$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

# Proteger os arquivos e diretórios
<FilesMatch "(\.(Search|CSS|Funcoes|Imagens|Paginas|JS|Modules|Fonts|.*sql|Instalacao|WebServer|Arquivos|Crontab(\.php)? |xtmpl)|code-style\.pl)$">
    Order allow,deny
</FilesMatch>

3 answers

0

Brother, you could try this :

RewriteRule ^([a-zA-Z0-9_\-]+)\.txt$ download.php?arquivo=$1

Anything started contains letters from a to z in lowercase and from A to Z in uppercase, underline or dash containing one or more characters preceded by . txt redirect to download.php passing as parameter get the name typed without the . txt.

Or you can do:

RewriteRule ^(.*)\.txt$ download.php?arquivo=$1

Anything preceded by . txt redirects to download by passing the get parameter the file name.

Note that if your server has Nginx in reverse proxy with apache and the extension (mime-type) has been set. txt to resolve on Nginx without redirecting to apache, this rule will not work. You must execute the rewrite directly in the Nginx vhost or remove the . txt extension from the Nginx-defined mime-types and resolve the rule in apache

vlw!

  • I’m using apache and not Nginx. Well it didn’t work out the way you said, I’ll edit the question with my htacess file.

0

What happens is that the rule you’re using is not redirecting, it’s rewriting the path, to redirect, you need to use the redirect rule, try doing it this way:

RewriteRule ^(.+)\.txt http://www.meusite.com.br/Download.php [R=301,L]

-1

The rule itself seems correct, already tried to mark it as passthrough [EN]

Type

RewriteRule ^(.*)\.txt$ download.php?arquivo=$1 [PT]

Browser other questions tagged

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