1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Secure web access using .htaccess rules
This guide explains how to block access to certain directories on a Web Hosting for certain visitors/robots/spiders by filtering and blocking their IP addresses or hostnames.
Setting up .htaccess Rules
Create a .htaccess
file at the root of your site and add rules to block certain IP addresses or bots.
To block visitors based on, e.g., the beginning of their IP address, use the "deny from
" directive:
Order Deny,Allow
Deny from 123.456.
Allow from all
This means that all requests from an IP address starting with "123.456.
" will be denied, while all other requests will be allowed. Blocked users will receive an HTTP 403 Forbidden
error message.
Block a Specific IP Address
Order Allow,Deny
Deny from 123.456.789
Allow from all
This means that a single IP address, 123.456.789
, is blocked and all other IP addresses can access the site.
Block Multiple IP Addresses
Order Allow,Deny
Deny from 123.456.789
Deny from 987.654.321
Allow from all
This means that two IP addresses, 123.456.789
and 987.654.321
, are blocked and all other IP addresses can access the site.
Block a Bot by Its User-Agent
SetEnvIfNoCase User-Agent "BadBot" BadBot
Order Allow,Deny
Deny from env=BadBot
Allow from all
This means that any bot identified with a user-agent "BadBot
" will be blocked, and all other users can access the site.
Block Access to a Specific Folder
Order Allow,Deny
Deny from all
This means that all access to the folder "/folder
" will be blocked, regardless of IP address or user-agent. An assistant for similar functionality is available in your Infomaniak Manager.
mod_rewrite Directives
You can also use the mod_rewrite
directive to block certain IP addresses or bots in an .htaccess file.
.htaccess
file.Here are some examples:
Block a Specific IP Address
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.456\.789\.
RewriteRule ^(.*)$ - [F,L]
This means that a single IP address, 123.456.789
, is blocked and all other IP addresses can access the site.
Block Multiple IP Addresses
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.456\.789\. [OR]
RewriteCond %{REMOTE_ADDR} ^987\.654\.321\.
RewriteRule ^(.*)$ - [F,L]
This means that two IP addresses, 123.456.789
and 987.654.321
, are blocked and all other IP addresses can access the site.
Block a Bot by Its User-Agent
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} BadBot [NC]
RewriteRule ^(.*)$ - [F,L]
This means that any bot identified with a user-agent "BadBot
" will be blocked, and all other users can access the site. This can be useful for preventing unwanted bots from accessing certain pages or consuming resources unnecessarily.
Block Access to a Specific Folder
RewriteEngine on
RewriteRule ^folder/secret - [F,L]
This means that all access to the folder "/folder/secret
" will be blocked, regardless of IP address or user-agent.
Block and Redirect Elsewhere
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^123\.456\.789\.
RewriteRule ^(.*)$ http://www.domain.xyz/blocked.html [L,R=301]
This means that all requests from the IP address 123.456.789
will be redirected to the page "blocked.html
" on the site "www.domain.xyz
". The last part of the RewriteRule
line, [L,R=301]
, indicates that the redirection is permanent (R=301) and that this is the last rule to be applied (L).
You can add multiple RewriteCond
conditions to block different IP addresses and redirect to different pages.
Also, read this guide (click here).