{"id":7705,"date":"2018-09-03T11:38:32","date_gmt":"2018-09-03T03:38:32","guid":{"rendered":"http:\/\/rmohan.com\/?p=7705"},"modified":"2018-09-03T11:38:32","modified_gmt":"2018-09-03T03:38:32","slug":"hotlink-protection","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7705","title":{"rendered":"Hotlink Protection"},"content":{"rendered":"<h3><a id=\"apache\"><\/a>Enable Hotlink Protection on Apache<\/h3>\n<p>If your WordPress site is running on Apache, all you need to do is open the .htaccess file in your site\u2019s root directory (or create it) and add the following:<\/p>\n<pre class=\" language-php\"><code class=\" language-php\">RewriteEngine on\r\nRewriteCond <span class=\"token operator\">%<\/span><span class=\"token punctuation\">{<\/span><span class=\"token constant\">HTTP_REFERER<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token operator\">!<\/span><span class=\"token operator\">^<\/span>$\r\nRewriteCond <span class=\"token operator\">%<\/span><span class=\"token punctuation\">{<\/span><span class=\"token constant\">HTTP_REFERER<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token operator\">!<\/span><span class=\"token operator\">^<\/span><span class=\"token function\">http<\/span><span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span><span class=\"token operator\">?<\/span><span class=\"token punctuation\">:<\/span><span class=\"token comment\">\/\/(www\\.)?yourdomain.com [NC]<\/span>\r\nRewriteCond <span class=\"token operator\">%<\/span><span class=\"token punctuation\">{<\/span><span class=\"token constant\">HTTP_REFERER<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token operator\">!<\/span><span class=\"token operator\">^<\/span><span class=\"token function\">http<\/span><span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span><span class=\"token operator\">?<\/span><span class=\"token punctuation\">:<\/span><span class=\"token comment\">\/\/(www\\.)?google.com [NC]<\/span>\r\nRewriteCond <span class=\"token operator\">%<\/span><span class=\"token punctuation\">{<\/span><span class=\"token constant\">HTTP_REFERER<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token operator\">!<\/span><span class=\"token operator\">^<\/span><span class=\"token function\">http<\/span><span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span><span class=\"token operator\">?<\/span><span class=\"token punctuation\">:<\/span><span class=\"token comment\">\/\/(www\\.)?bing.com [NC]<\/span>\r\nRewriteCond <span class=\"token operator\">%<\/span><span class=\"token punctuation\">{<\/span><span class=\"token constant\">HTTP_REFERER<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token operator\">!<\/span><span class=\"token operator\">^<\/span><span class=\"token function\">http<\/span><span class=\"token punctuation\">(<\/span>s<span class=\"token punctuation\">)<\/span><span class=\"token operator\">?<\/span><span class=\"token punctuation\">:<\/span><span class=\"token comment\">\/\/(www\\.)?yahoo.com [NC]<\/span>\r\nRewriteRule \\<span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">(<\/span>jpg<span class=\"token operator\">|<\/span>jpeg<span class=\"token operator\">|<\/span>png<span class=\"token operator\">|<\/span>gif<span class=\"token operator\">|<\/span>svg<span class=\"token punctuation\">)<\/span>$ http<span class=\"token punctuation\">:<\/span><span class=\"token comment\">\/\/dropbox.com\/hotlink-placeholder.jpg [NC,R,L]<\/span><\/code><\/pre>\n<p>The second line allows blank referrers. You will most likely want to enable this as some visitors use a personal firewall or antivirus program that deletes the page referrer information sent by the web browser. If you don\u2019t allow blank referrers, you could inadvertently disable all of your images for those users.<\/p>\n<p>The third line defines the allowed referrer, the site that is allowed to link to the image directly, this should be your website (update yourdomain.com above with your domain). The fourth, fifth, and sixth lines add search engines to the allowed list, because you don\u2019t want to block crawlers such as Google bot or Bing bot. This could prevent your images from showing and indexing in Google image search.<\/p>\n<p>And the seventh line defines the image you want the visitor to see in place of the hotlink protected image. This not required, but you could give them a friendly warning. If you want to allow multiple sites you can duplicate this row and replace the referrer. If you want to generate some more complex rules, take a look at this <a href=\"http:\/\/www.htaccesstools.com\/hotlink-protection\/\" target=\"_blank\" rel=\"noopener\">htaccess hotlink protection generator<\/a>.<\/p>\n<p>If you are using the above rules along with a CDN, you might also need to whitelist your CDN subdomain.<\/p>\n<h3><a id=\"nginx\"><\/a>Enable Hotlink Protection on NGINX<\/h3>\n<p>If you are <a href=\"https:\/\/kinsta.com\/knowledgebase\/what-is-nginx\/\" target=\"_blank\" rel=\"noopener\">running on NGINX<\/a>, all you need to do is open\u00a0your config file and add the following:<\/p>\n<pre class=\" language-php\"><code class=\" language-php\">location <span class=\"token operator\">~<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">(<\/span>gif<span class=\"token operator\">|<\/span>png<span class=\"token operator\">|<\/span>jpeg<span class=\"token operator\">|<\/span>jpg<span class=\"token operator\">|<\/span>svg<span class=\"token punctuation\">)<\/span>$ <span class=\"token punctuation\">{<\/span>\r\n     valid_referers none blocked <span class=\"token operator\">~<\/span><span class=\"token punctuation\">.<\/span>google<span class=\"token punctuation\">.<\/span> <span class=\"token operator\">~<\/span><span class=\"token punctuation\">.<\/span>bing<span class=\"token punctuation\">.<\/span> <span class=\"token operator\">~<\/span><span class=\"token punctuation\">.<\/span>yahoo<span class=\"token punctuation\">.<\/span> yourdomain<span class=\"token punctuation\">.<\/span>com <span class=\"token operator\">*<\/span><span class=\"token punctuation\">.<\/span>yourdomain<span class=\"token punctuation\">.<\/span>com<span class=\"token punctuation\">;<\/span>\r\n     <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token variable\">$invalid_referer<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span>\r\n        <span class=\"token keyword\">return<\/span>   <span class=\"token number\">403<\/span><span class=\"token punctuation\">;<\/span>\r\n    <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<p>If you are a Kinsta user and aren\u2019t using a CDN, we can add this for you. Just open up a quick ticket with our support team from the <a href=\"https:\/\/my.kinsta.com\/\" target=\"_blank\" rel=\"noopener\">MyKinsta<\/a> dashboard.\u00a0If you are using the above rules along with a CDN, you might also need to whitelist your CDN subdomain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enable Hotlink Protection on Apache <\/p>\n<p>If your WordPress site is running on Apache, all you need to do is open the .htaccess file in your site\u2019s root directory (or create it) and add the following:<\/p>\n<p> RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?yourdomain.com [NC] RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?google.com [NC] RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?bing.com [NC] RewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?yahoo.com [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7705"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7705"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7705\/revisions"}],"predecessor-version":[{"id":7706,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7705\/revisions\/7706"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}