Restricting direct access to files using .htaccess - .htaccess

I have video files in a folder. And I want to restrict the users from directly accessing them. I have my .htaccess like this
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(mp4|flv)$ - [F]
IDEA:
This will allow the browser to access my video when requested from page. But it will deny direct access from url.
Actual response:
Its working fine for Chrome. But in all other browsers (firefox, opera, midori) Its giving StreamNotFound error. What is the issue? Any help please.

Try this rule:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteRule \.(mp4|flv)$ - [NC,F]

Related

What changes should I have to implement in my .htaccess if I want to allow specific URL but prevent on all others?

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?site1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?site2\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^$
RewriteRule \.(jpg|jpeg|png|gif|js|css)$ - [NC,F,L]
The above code is doing the opposite for me. I want to a site1 and site2 but prevent all others.
You should have your .htaccess rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
RewriteCond %{HTTP_HOST} ! ^https?://(?:www\.)?(?:site1|site2)\.com$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|js|css)$ - [NC,F,L]

.htaccess allow hotlinking for one specific folder

I'll get straight to it. I have this code in my .htaccess file to prevent hotlinking, see below:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://mywebsite.ca/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://mywebsite.ca$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mywebsite.ca/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mywebsite.ca$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]
but I will have a few .jpg images up for banner exchange between sites and I don't know how to write the rule to allow hotlinking for just one folder. In this case the one folder will be root/image/links/
What changes in the rewrite conditions do I need to make to allow hotlinking for this one folder?
Thanks for your anticipated help everyone.
You can use
RewriteCond %{REQUEST_URI} !^/image/links/
as an additional condition.

Redirect a test link to an old one if coming from Google

So, while doing a project for a client, some trouble with the .htaccess lead to a test URL being referenced by Google. Which is not super...
So, I was wondering if it was possible to redirect to the live URL when somebod
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} googlebot [NC,OR]
RewriteCond %{HTTP_REFERER} ://[^.]+\.google\. [NC]
RewriteRule ^test/page\.php$ http://domain.com/live/page.php [R=301,NC,L]
You can have this rule:
## check if it is coming from google
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?google\. [NC]
RewriteRule ^test/?$ /live/ [NC,L,R=301]

How to exclude certain filenames from being Hotlinked Blocked in .htaccess

I've currently got the following in my .htaccess of my photo folder.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?siteabc.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sitexyz.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://example.com/images/bandwidth.png [NC,R,L]
This blocks all not allowed domains from hotlinking our photo files and displays the http://example.com/images/bandwidth.png in it's place.
However, what I would like to do is to allow sites to be able to hotlinked our thumbnails. The thumbnail images have this sort of filename format
filenameabc_100_cw100_ch100_thumb.jpg
filenameabc_100_cw100_ch100_thumb.png
filenamexyz123_100_cw100_ch100_thumb.png
eg the filenames all end with _thumb.ext
So what I would like to do is to modify the above .htaccess to globally block all access with the exception of the filenames ending in thumb eg *_thumb.jpg or *_thumb.png
I don't have the first idea about how to write such a rule.
If anyone has any ideas I would be most grateful.
Modifying the rule-set in the question:
You may try this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?siteabc.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sitexyz.com [NC]
# Add this other exception
RewriteCond %{REQUEST_URI} !_thumb\. [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://example.com/images/bandwidth.png [NC,R,L]

htaccess hotlink protection

So I'm trying to protect FLV files using htaccess.
Basically I want to block direct access to the .flv (so blank referrer) and block hotlinking from any website that isn't my own.
I'm doing this from a WordPress install but it's not on the root, it's in a subdirectory. so mydomain.com/test/videos/
I've tried the following code but I am still able to type a direct filename of the flv and chrome will download the file, so it isn't working:
RewriteEngine on
RewriteBase /test/videos/
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain.co.uk(/)?.*$ [NC]
RewriteRule \.(flv)$ templates/images/logo.jpg [L,NC]
Any ideas where this is going wrong? it doesn't return a server error.
Try this one:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain.co.uk/test/videos [NC]
RewriteRule \.(flv)$ - [NC,F,L]

Resources