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 ?

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

3 Comments

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, 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.
Sure. The question is here: stackoverflow.com/questions/70504461/…
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.