skip to Main Content

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 ?

2

Answers


  1. 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]
    
    Login or Signup to reply.
  2. 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]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search