Avatar of andy7789
andy7789
 asked on

how to prevent direct http requests via .htaccess

Hi X-perts,

I need to protect a few directories from direct http requests. Currently, I have the following .htaccess file in those directories:

<Files *.php>
Order Deny,Allow
Deny from all
</Files>

It prevents from executing any php or asp files, but not from viewing .css or .js files. How can I protect those directories from viewing files?

I tried also:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|js|css)$ - [F]

<Files *.php,*.css,*.js>
Order Deny,Allow
Deny from all
</Files>

But it does not work.

Please, advise.

Thanks

Andy
ASP.NETPHPApache Web Server

Avatar of undefined
Last Comment
ddrudik

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ddrudik

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Oscurochu

You can have .htaccess do a referer check. on the javascript and css files. If no referer is defined, then deny access.
andy7789

ASKER
ddrudik, I have tried this and it really blocks all direct requests, but due to some reasons also blocks the site's own css links to the css files, if they are in the same folder as .htaccess file. It works ok, if i move css file to a subdirectory below. Why is that?
ddrudik

> but due to some reasons also blocks the site's own css links to the css files, if they are in the same folder as .htaccess file.

Blocking access to the CSS files in this manner also would block the site's use of them as well.  No way around that.

>It works ok, if i move css file to a subdirectory below. Why is that?
Check for .htaccess files throughout your directory structure, for example Apache checks for .htaccess files starting at root of the site down to the folder in which the request was made to apply allow/deny commands in the .htaccess files.
Your help has saved me hundreds of hours of internet surfing.
fblack61
HackneyCab

Try changing your second rule to:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.com

Does that work?
andy7789

ASKER
no it does not, So far, the ddrudik's solution works, though it generates some odd restrictions to the root dir files
ddrudik

Thanks for the question and the points.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.