Search code examples
apache.htaccesshttp-redirectmod-rewritehttp-status-code-401

How to prevent user to access images in multiple directories via the url


I am using .htaccess to prevent user to access images through url.
Thanks to this question, I could prevent users to access .js files.

RewriteEngine ON
RewriteRule ^frontend/assets/(?:js) - [R=401,NC,L]

However, when I applied the same rules in order to denied access to dir1 and dir2, the access is only denied to images in directory dir1.

RewriteRule ^frontend/assets/images/(?:dir1|dir2) - [R=401,NC,L]

Is there a way to handle this and prevent users to access files in directory dir2 as well ?


Solution

  • Please try following htaccess rules file. Apart from fixing regex we need to be careful on where we need to place the rule. So place this rule before your previously used rule(s).

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

    ##Enabling RewriteEngine here...
    RewriteEngine On
    ##Rewrite rule for images with checking users OR uploads here...
    RewriteRule ^free/frontend/assets/images/(?:users|uploads) - [R=401,NC,L]
    ##Rewrite rule for images OR css OR js folders here....
    RewriteRule ^frontend/assets/(?:images|css|js) - [R=401,NC,L]