gravityboy
gravityboy

Reputation: 817

Allow .htaccess only from one directory to another

I have mp3's in a directory called /mp3/ and I want to be able to access them only from another page.php in another directory /main/ on the site. No direct linking from outside.

All of the pages are written in php

I put this code in the .htaccess file inside the /mp3/ directory...

Order deny,allow
deny from all

allow from 127.0.0.1
allow from localhost
allow from mydomain.com
allow from 123.45.678.90  # that's myserver's IP address (real one used in code)

Satisfy Any

But none of those work. It does work however if I use the IP address of were I am.

allow from 1.2.3.4  # my internet connection (real one used in code)

But that means it would work for anyone and their IP address.

What am I missing here? Does this work only on certain servers? How do I make it use the server's IP address and not my IP address?

Upvotes: 1

Views: 878

Answers (5)

Phil Perry
Phil Perry

Reputation: 2130

Look into "hotlink protection" added to your .htaccess file. You can set it up for just .mp3 file extension, and forbid access by any foreign site or directly from browsers. You might even be able to restrict access from within your own site, but I can't see that being terribly useful.

Something like

RewriteEngine on
Options +FollowSymlinks
# hotlink protection and allowed list
# don't forget to add https: to allow accesss for any with SSL
##  uncomment following line to PERMIT direct browser access of mp3 files
#RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com(/)?.*$     [NC]
RewriteRule .*\.mp3$ - [F,NC]

Upvotes: 1

anubhava
anubhava

Reputation: 785098

Place this code in your mp3/.htaccess file:

RewriteEngine on
RewriteBase /mp3/

RewriteCond %{HTTP_REFERER} !^https?://(localhost|(www\.)?mydomain\.com)/ [NC]
RewriteRule ^ - [F]

Upvotes: 0

Arian Faurtosh
Arian Faurtosh

Reputation: 18511

What about something like this, in your .htaccess

<Files ~ ".mp3$">
    Order allow,deny
    Deny from all
</Files>

This will not allow direct access to any files with .mp3 from your web server.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143886

You are trying to limit a "referral" and not direct access?

Denying from an IP limits all access, whether referred to by your page.php or by typing it into the browser's URL location bar. If you're trying to limit referrals, you can try using something like:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain.com/ [NC]
RewriteRule ^mp3/ - [L,F]

but be warned that referers can be spoofed.

Upvotes: 0

Lohardt
Lohardt

Reputation: 1045

Place the files you want to protect out of the public folder. This way they are only accessible via your scripts.

-root
--mp3s
--public_html
---main
----index.php
----page.php

Upvotes: 0

Related Questions