.htaccess allow embedding but redirect on direct image load - image

Right now I want to redirect users who open up a direct image thats embedded in another site.
livememe does this perfectly by having http://www.livememe.com/36opcf5.jpg redirect to http://www.livememe.com/36opcf5 even though the direct image url is embedded within another site.
Now I'm trying to acomplish the same thing and this is what I have so far:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?reddit.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?tumblr.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?facebook.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mysite.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} ^$
RewriteRule ^i/([a-zA-Z0-9]+)\.(jpe?g|JPE?G)$ /r/$1 [R]
Now from what I understand is that I'm allowing these websites to embed my images in their site but I'm redirecting them if they access the url directly without being refered.
For example on my site I have images located like this: http://mysite.com/i/0b1be.JPG
While the article is located: http://mysite.com/r/0b1be
The redirect works but I'm still failing because you can still access the direct image if you followed it from any of the websites allowed.

You can try this code :
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?(reddit|tumblr|facebook|mysite).com/.*$ [NC]
RewriteRule ^i/([a-zA-Z0-9]+)\.(jpe?g|JPE?G)$ /r/$1 [R]

You could try to add this condition:
RewriteCond %{HTTP_ACCEPT} !^(.*)image(.*)$ [NC]

Related

Prevent hotlinking image files

I want to avoid hotlinking images. Basing of this answer: stackoverflow answer I've tried to add the next snippet code into .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?itransformer.es/.*$ [NC]
RewriteRule \.(gif|jpe?g|png|wbmp)$ htttp://itransformer.es [R,L]
but it is not working. When I try to access the images putting the path in the navigation bar, I can access. What am I doing wrong?
You're explicitly ignoreing referers that are blank. When you type the URL in your navigation bar, there is no referer, therefore %{HTTP_REFERER} is blank and it will fail the !^$ pattern. Try this instead:
RewriteCond %{HTTP_REFERER} ^$ [OR]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?itransformer.es/.*$ [NC]
RewriteRule \.(gif|jpe?g|png|wbmp)$ http://itransformer.es [R,L]
Also, you have an extra "t" in your htttp://

Dynamically prevent hotlinking images using htaccess for multiple domains

Preventing hotlinking using htaccess is well documented. However, I want to prevent hotlinking for multiple domains without adding a rule per domain.
My idea is to match the referrer with the hostname, this seems like a good solution to me.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?%{HTTP_HOST}/.*$ [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
</IfModule>
Is this is a proper and safe solution to prevent hotlinking?
This won't work when the request comes with www. but the referrer doesn't. That's because your rule would effectively try to match the following which wouldn't work.
RewriteCond http://domain.com/index.php !^http://(www\.)?www\.domain\.com/.*$
The correct way is to use the following:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^https?://(www\.)?([^/]+)/.*$ [NC]
RewriteCond %2#%{HTTP_HOST} !^(.+)#(www\.)?\1$ [NC]
RewriteRule \.(bmp|gif|jpe?g|png|swf)$ - [F,L,NC]
This takes care of SSL (https:) as well. Take a look here to see how it works.

mod_rewrite forward shortend URL

I am looking for a way to create a short URL path for a longer URL on my page
the long url is: domain.com/tagcloud/user.html?t=1234ABCD
i would like to offer a short version of the URL to easy access it:
domain.com/t/1234ABCD
I tried a few examples but I just don't get it how I could forward these rules.
RewriteRule ^(.*)/t/$ /tagcloud/user.html?t=$1 [L]
I am also using MODX so they already use rules.
in addition my htaccess file
RewriteEngine On
RewriteBase /
# Always use www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I must keep the code snippets above in my htaccess file. The first one simply forwards http://domain.com requests to www.domain.com
The friendly URLs part is needed to translate the internal IDs of my CMS with the alias of the URL. This feature must remain because the entire site cannot be influencted by the changes I try to make in htaccess...
I simply would like to add a listener that only if the URL matches www.domain.com/t/abcd1234
Therefore I need something that identifies the www.domain.com/t/ URL
your help is much appreciated
Try this:
RewriteCond %{REQUEST_URI} ^/t/.*
RewriteRule ^t/(.*)$ /tagcloud/user.html?t=$1 [R=301,L]

How to rewrite exact url while passing other paths through

I'm new to mod_rewrite, so I know this is probably a simple fix. I want to rewrite www.abc.com to www.xyz.com ONLY. I only want to do this in firefox. I do not want www.abc.com/def to redirect to www.xyz.com. Currently, I have the following:
RewriteCond %{HTTP_HOST} ^www.abc.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} "firefox" [NC]
RewriteRule ^(.*)$ https://www.xyz.com [L,R=302]
I understand that the ^(.*)$ portion is what is allowing everything else to be redirected as well. What should I enter in there that will ONLY redirect www.abc.com?
Thank you!
Change the rule to
RewriteRule ^/?$ https://www.xyz.com [L,R=302]

Mapping a secondary domain to a subdirectory using mod_rewrite

I am looking to map a secondary domain to a subfolder on my document root.
For example, if requests to the domain www.example.com map to my DocumentToot, then requests to www.exampletwo.com go to /sites/files/.
I am unable to accomplish a redirect from www.exampletwo.com/index.html to www.exampletwo.com/sites/files/index.html while making the URL still display www.exampletwo.com/index.html. Any ideas?
I believe you're looking for something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) /sites/files/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/sites/files/$1 [L,P]
The P flag uses the proxy module, therefore the url is not changed (no redirect) on the client.

Resources