Tag: htaccess

How to Add Hotlink Protection using .htaccess in WordPress

Adding this snippet into your .htaccess file, you will be able to add hotlink protection to your files. It will help you to save your site bandwidth.

RewriteCond %{HTTP_REFERER} !^http://example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

Here hotlink has been added to image files (.jpg, .jpeg, .gif, .png, .bmp). You can add or remove more file types.

RewriteCond %{HTTP_REFERER} !^http://example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://example.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com$      [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|zip)$ - [F,NC]

Remember to replace “example.com” with your domain name. Look at the example below:

Add Hotlink Protection using htaccess in WordPress

How to Block Visitors using .htaccess in WordPress

To block a visitor in your WordPress site using this method, you need to know the IP address of that visitor. Adding this snippet into your .htaccess file, that user will see a error message when trying to access into your site.

<Limit GET POST PUT>
order allow,deny
allow from all
deny from x.x.x.x
</Limit>

Remember to replace “x.x.x.x” with the IP address of the visitor you want to block.

If you need to block more than one visitor, then add their IP addresses like this:

<Limit GET POST PUT>
order allow,deny
allow from all
deny from x.x.x.x
deny from y.y.y.y
deny from z.z.z.z
</Limit>

How to Protect wp-config.php with .htaccess

Adding below snippet into your .htaccess file will prevent access to your wp-config.php file:

<Files wp-config.php>
 Order Allow,Deny
 Deny from all
</Files>