M
M
Mesuti2018-10-24 15:40:30
htaccess
Mesuti, 2018-10-24 15:40:30

How to redirect access to a file?

Hey!
how to redirect access to a file?
For example, there is a link site.ru/file.rar
When accessed, it should be loaded from site.ru/disk/file.rar

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2018-10-24
@shambler81

RewriteCond %{REQUEST_URI} !\.(css|js|jpg|gif|png)$ - file mask
RewriteCond %{REQUEST_FILENAME} -f without mask is just file
RewriteCond %{REQUEST_FILENAME} robots.txt$ - specific file
Your rule

RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteCond %{REQUEST_FILENAME} file.rar$
RewriteRule ^/(.+\..+)$ /disk/$1? [L,R=301]

or
RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteCond %{REQUEST_FILENAME} -f   
RewriteRule ^/(.+\..+)$ /disk/$1? [L,R=301]

and the like
. Give more options. Let's make the regular season more beautiful, whether there are only files in the root, whether any files need to be transferred to disk, and what about those nested in other directories, and so on.
PS
^/(.+\..+)$ /disk/$1?
^ the beginning of the line
*(....) - what will become the first group $1
.+ - the same as .* only not from 0 characters but from 1
\ character escaping (that is, the character itself and not what it means in the regular expression
? at the end - will remove the get parameters if they exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question