htaccess to block direct access to images and pdfs - .htaccess

I'm trying to prevent a user from directly surfing to an image that is on my website.
Example
prevent access
www.mydomain.com/images/myimg.jpeg
mydomain.com/images/myimg.jpeg
Allow the image to be shown if its being viewed from within my website
<img src="www.mydomain.com/images/myimg.jpeg">
<img src="mydomain.com/images/myimg.jpeg">
<img src="/images/myimg.jpeg">
I'm confident that its not a cache issue because I'm uploading the image on one computer and trying to call it on another which has never called the image before?.
I'm using this in my .htaccess file but it doesnt appear to be blocking the images?
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(jpg|pdf|jpeg|png)$ - [F]
NOTE if I put php as a file type and try and surf to my .php page i get forbidden message I'm hoping to get when trying to directly access an image or pdf.
What am I missing here?

Related

deny access to folders, but display content on the webpage

I want to block the video folder,so if someone would use the direct link of the video path in the DB, they would get the error.
But I want to display that video on my webpage.
So I denied the access of that folder in htaccess file as follows:
RewriteEngine On
RewriteRule (^|/)videos(/|$) - [F]
But the videoplayer is just blank.(because of the 403 error)
Is this the right way? Can I allow my own page to access it? Or I should do it some other way?
I think the best solution for displaying videos but lock it out from the public would be to use a CDN (Content Delivery Network) like Cloudflare to deliver and stream your videos and disallow the public to view all your videos in a folder.
Here is more information on Cloudflare's Video Hosting solution.
https://www.cloudflare.com/products/cloudflare-stream/
I hope that helps answer your question.
Cheers!
Found soulution, create new htaccess file and add it to your media folder. Allows access only from your site. Blocks direct request from elsewhere.
Put this into the file:
RewriteEngine On
RewriteCond %{REQUEST_URI} .(mov|png|mp4)$ [NC]
RewriteCond %{HTTP_REFERER} !^https://yoursite.com/.*$ [NC]
RewriteRule ^.* - [F,L]

need to block direct access xml using htaccess still need to access through kodi app

i have lot of xml files on my server i need to block direct access of xml file like if someone copy xml url and need to redirect somewhere but i need to access that xml through kodi app. i got success to direct access of xml but that blocking kodi access also. please check below code is my htaccess which blocks direct access.
ErrorDocument 403 http://newdomainexample.com/
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com.*$ [NC]
RewriteRule \.(xml)$ - [F]
above htaccess also blocking htaccess from kodi. i need kodi can access url but people cant access directly.
any solution please

Redirect direct image access

Im struggling with this problem and i know there are some solutions but none is working fine for me. I want to redirect direct image access to its HTML site because the image filename is exact the same like the HTML site. Example:
http://example.com/images/subcategory/this-is-an-image.png
should be redirected to
http://example.com/images/subcategory/this-is-an-image.html
The image can be an png, jpg, jpeg or gif. And of course i still want to embedd the images in <img> tags on my site. The goal is not to prevent hotlinking, the goal is to redirect all direct image calls on my server to its html site. Are there any solutions? Thank you.
You can use that:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)example\.com [NC]
RewriteRule ^(.+)\.(jpe?g|gif|png)$ $1.html [NC,R,L]
Not 100% reliable method, but better than nothing...

How to prevent downloading using htaccess

I have some video files few of which are not public. I want to access them through video player but I also need to prevent the direct download from the address bar. Also if a user pays for it they can download.
I tried in .htaccess file but still not solved.
I tried the following:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^(https?://)?yoursite.com
RewriteRule ^/?video_files - [L,F]

.htaccess image to web page redirect?

Is there a .htaccess script I can use to redirect the url of an image to an actual web page?
What can I do to make it so when someone accesses an image via their address bar by typing in: http://www.sitename.com/images/1.jpg
It will instead redirect the user to the web page:
http://www.sitename.com/view/1.html
I want to still be able to place the images in image tags though.
RewriteCond %{HTTP_REFERER} !^https?:\\www\.yourdomain\.com
RewriteCond %{REQUEST_URI} (.*)\.jpg$
RewriteRule images/(\d+)\.jpg http://www.yourdomain.com/view/$1.html [L,R=301]
by using this you can put images in img tag and it will work but If you try to access them directly , it will redirect to html file.
Note: If you want others have permission to use your images use this:
RewriteCond %{HTTP_REFERER} ^$

Resources