Block visitors by IP address using .htaccess

Deny access based upon IP address, or an IP block, by placing the following code into your .htaccess file.

#Block access to IP address and to IPs beginning with 192.168.169.
order allow,deny
deny from
deny from 192.168.169.
allow from all

#Block access to All visitors except yourself (What is My IP Address)
order allow,deny
allow from
deny from all


Using mod_rewrite is an alternative method for blocking IP addresses with .htaccess:

#Block access to IP address
RewriteEngine on
RewriteCond %{Remote_Addr} ^192\.168\.169\.170$
RewriteRule ^(.*) http://localhost/ [R,L]

#Block access to IP addresses from to
RewriteEngine on
RewriteCond %{Remote_Addr} ^192\.168
RewriteRule ^(.*) http://localhost/ [R,L]

Was this answer helpful?

Also Read

.htaccess URL Masking Examples

If you would like to access a specific URL for your domain but do not display it in the browser's...

Do you support mod_rewrite for apache?

Yes, mod_rewrite is enabled on your server. For more information on what mod_rewrite is and how...

Leverage Browser Caching using .htaccess

Add the following .htaccess rules to the very end of your .htaccess file to reduce the load times...

.htaccess RewriteRule Examples

Here are some useful mod_rewrite RewriteRule redirect examples that you can use in your .htaccess...

Block Bad Bots and Spiders using .htaccess

Below is a useful code block for blocking a lot of the known bad bots and site rippers currently...