Redirect direct image access - .htaccess

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...

Related

htaccess rewrite url, add dir and remove query

I have two versions of images on my server, one with and one without a watermark. On my one website I want to have to non-watermarked version. On the websites that hotlink my images I want to have the ones with the watermark.
I want to rewrite the url to serve the watermarked images on those website. The url structure on my website is this:
www.example.com/wp-content/uploads/image-name.jpg?t=1512472796
and I want to change it to:
www.example.com/wp-content/uploads/wm/image-name.jpg
I am kind of stuck here, no idea to do the part that adds the wm dir to the url and removes the query:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{QUERY_STRING} ^t=([0-9]*)$
Any help is highly appreciated.

.htaccess rewrite site with #item in URL for direct file access

I hope someone can help me, please. I created a site with ajax and I want if someone visits www.example.com/example1.html to get redirected to
www.example.com/#items/example1.htmlI need to redirect them to the /#items at the beginning of the URL for many HTML files because if someone visits the direct HTML file the site is broken (no CSS & JS is in the HTML files) because I created the HTML files only for ajax calls and want to prevent direct URL access for better user experience. I hope someone here might know how to get such a redirect using a .htaccess file.
EDIT
I tried this before with no success. I don't know how to do this redirect, to be honest :)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ /#items/$1.html
Use RewriteRule directive:
RewriteRule ^(.*).html$ /#items/$1.html [NE,R=301,L]

htaccess to block direct access to images and pdfs

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?

.htaccess file for pretty urls

(This seems like it should be one of the mostly commonly and easiest addressed questions on the web, since most websites have "pretty" or "clean" urls. But in all my searches, it's proven to be one of the most complex.)
In the simplest form, I would like be able to enter example.com/about into the url bar and have the server return the file example.com/about.php. As it is, I have to enter or link to example.com/about.php, which is not SEO or user friendly. This isn't about complex strings--the file could just as easily be example.com/about.html.
I have some code I'm attempting to use with an .htaccess file, but it seems to do nothing:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/$ /$1.php
I know that the .htaccess file is working, because the 404 redirect I have set up (which appears in the .htacces doc below the code I've included here) is functioning properly, especially when I'm trying access example.com/about and I get my 404 page.
Thanks for your help!
Your last rule is designed to match http://example.com/about/ . I think what you want is
RewriteRule ^([a-z]+)$ $1.php

.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