user3106347
user3106347

Reputation: 658

Folder permission .htaccess

I am creating a php website, I want to protect my css file. So that if any user tried to open it directly in browser, he won't be able to see the code for this. I have created .htaccess file and wrote

 # no one gets in here!
deny from all

ErrorDocument 403 "You dont have sufficient priviliges to view this page.

But now my css is not at all rendering in any pages. How can i acheive this functionality?

Upvotes: 1

Views: 2560

Answers (2)

anubhava
anubhava

Reputation: 785551

Not 100% foolproof but you can do this via HTTP_REFERER check.

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com/ [NC] 
RewriteRule \.(css|js|jpe?g|gif|bmp|png)$ - [F,NC]

Replace yourdomain\.com with your actual domain name.

Now your own pages will be able to access css, js, image files but externally these files cannot be accessed

Upvotes: 2

Alexey Kamenskiy
Alexey Kamenskiy

Reputation: 2948

Protecting files via .htaccess, will prevent them from opening via browser. Therefore when browser will try to load this file to render CSS it will not be able to access it.

Somehow obvious - no?

In addition - CSS, HTML, JavaScript files cannot be protected from opening them in browser, because in order to serve their purpose those should be accessible from browser...

Upvotes: 3

Related Questions