2

I am using .htaccess to prevent user to access files through url.
The line

RewriteRule ^frontend/assets/css/(.+) index [L,R]

works fine. It prevents users to access css files.
However,

RewriteRule ^frontend/assets/images/(.+) index [L,R] //Images

as well as

RewriteRule ^frontend/assets/js/(.+) index [L,R] //Javascript files

do not work.
I have tried multiple solutions but still I can access js and images (png and svg) files from the url.
Is there a way to handle this ?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Kyv
  • 615
  • 6
  • 26

2 Answers2

2

Please following rules at the top of your htaccess rules file. You need not to create 3 separate rules for 3 conditions, you can use regex here and can do a 401 redirect for all of 3 strings(js/images/css) in a single rule itself.

Make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteRule ^frontend/assets/(?:images|css|js) - [R=401,NC,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thank you @RavinderSingh13. I am facing another issue. There are 2 directories in the image directory. When I do `RewriteRule ^frontend/assets/images/(?:dir1|dir2) - [R=401,NC,L]`, the access is only denied to one directory. – Kyv Dec 28 '21 at 08:11
  • @Kyv, sure, could you please open a new question for same, we could discuss it there but please post your efforts(like you mentioned here code), cheers. – RavinderSingh13 Dec 28 '21 at 08:13
  • Sure. The question is here: https://stackoverflow.com/questions/70504461/how-to-prevent-user-to-access-images-in-multiple-directories-via-the-url – Kyv Dec 28 '21 at 08:26
0

actually, this helped me: Allow/deny image hotlinking with .htaccess

small video with the same example: https://www.youtube.com/watch?v=PsbpuH_e12E

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?domain\.com [NC]
RewriteRule \.(gif|jpeg|png|jpg|js|css)$ - [F,NC,L]
Axylo
  • 13
  • 2