Albi Patozi
Albi Patozi

Reputation: 1468

use htaccess to deny access to a js file if it is opened directly from the url

i am a noob in htaccess so i want some help please ! i want to deny access to a js file if it is opened ftom the url bar ex ( when i type http://mysite.com/js/jquery.js in the address bar - i get a 403 ) but when the site is rendered by the browser it is downloaded to the browser normally .

for the moment i was trying this

   <Files ~ "(.js|.css)">
    Order allow,deny
    deny from all
    </Files>

but this prevents the browser to use the css and the js so i want to make a kind of rule to let the browser but not anyone directly from the url . can in this case use the referer or the host or something like this ?

(note : i don't want to prevent the clients from viewing the js code because they could do it anyway but i have my reasons)

Upvotes: -1

Views: 9217

Answers (3)

Mareks Dunkurs
Mareks Dunkurs

Reputation: 11

Reddit post https://stackoverflow.com/a/10660862/4766489 Correct answer:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http(s)?://(.+\.)?mysite\.com/ [NC]
RewriteRule .*\.(js|css)$ - [NC,F,L]

Upvotes: 0

Marduk
Marduk

Reputation: 101

Set in your index.php a Cookie value, that indicates your js.php file (Store your js Code in a php file und echo it to the browser) if a request was made on your main page. If the cookie is not set, send a 404 otherwise echo the js code.

Upvotes: 1

freejosh
freejosh

Reputation: 11383

This would be the same as preventing image hotlinking. See http://altlab.com/htaccess_tutorial.html.

i.e:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteRule .*\.(js|css)$ - [F]

Upvotes: 3

Related Questions