Block web access via .htaccess

Aggiornato il 2026-10-23 05:01:23 87 visite

In this manual, we will explain how to deny access to someone on our website by editing the .htaccess file.

Connect via FTP to the directory '/datos/web/' and edit the file '.htaccess'.

info The file '.htaccess' is hidden. If you do not see it, you must activate the option to see hidden files in your FTP client. It is also possible that you may need to create it.

Below, add the lines of code that we specify according to the needs of your website.

Deny access to a specific IP

With this first code, you will be able to block access to a user by their specific IP address. In this way, when the user wants to connect, a 403 Forbidden error will be shown:

deny from 173.236.241.100

You may need to block an entire block of IPs (we do not recommend this). To do this, you must leave the last octet unspecified; this way, you deny access to anyone who has that IP in the range from 173.236.241.0 to 173.236.241.255:

deny from 173.236.241.

Allow access only to a specific IP

If, on the other hand, you need it so that no one can access your website, but you or someone else must be able to see it to work on it, you can block access to all IPs except for one specific IP with the following code:

order deny,allow
deny from all
allow from <TU_DIRECCION_IP>

Deny access to a specific domain

With this, we will make it so that anyone connecting to your site from a specific domain, for example 'www.swmanuales.com', cannot access it. There are three ways to carry this out:

403 forbidden

If someone clicks on a link on swmanuales.com that redirects to your site, they will see a 403 Forbidden error:

SetEnvIfNoCase Referer 'swmanuales.com' bad_referer
Order Allow,Deny
Allow from ALL
Deny from env=bad_referer

500 internal server error

A 500 Internal Server Error will be shown to deny any web request that comes redirected from the domain swmanuales.com:

RewriteEngine on
RewriteCond %{HTTP_REFERER} swmanuales\.com [NC,OR]
RewriteRule .* - [F]

Redirection to google.com

With this code, any visitor from the URL http://swmanuales.com is redirected to the URL http://www.google.com:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://swmanuales.com/
RewriteRule /* http://www.google.com [R,L]

Deny access to files

With this code, we are going to deny access to any file with the extension we indicate. Instead of viewing the content, a 404 error will be seen. In the example, the code forces any file ending in .inc to give the 403 error when visited:

<Files ~ '\.inc$'>  
Order Allow,Deny
Deny from All
</Files>

Deny access to 'hidden' files

Hidden files, such as the .htaccess file, are those that start with a dot '.'. These types of files are not visible to visitors. You can recursively deny all access to all files by putting the following code at the top of your .htaccess:

RedirectMatch 403 /\..*$

Deny access to directories

If you do not have an index file in your directory, all your files are listed in a directory list for anyone to see. Usually, we always have an 'index.html or index.php' file in our service, but we may need to deny access for security reasons. You can deny access to a specific folder or directory with the following code so that when a visitor accesses the directory list, a 404 Forbidden error is given:

Options -Indexes

Deny access to a specific directory

If you have a specific directory that you do not want users to access, you should use the following code. We will use, for example, that the directory we want to block is called 'swmanuales':

RewriteEngine On
RewriteRule (^|/)swmanuales(/|$) - [F]

Deny access during a specific hour of the day

In this way, we will deny access to our website during a specific hour of the day. We can specify one or several hours.

Example, specifying that access is not allowed from 15:00 to 15:59:

RewriteEngine On
# If the hour is 15 (3 PM)
RewriteCond %{TIME_HOUR} ^15$
# Deny all access
RewriteRule ^.*$ - [F,L]

Example, specifying that access is not allowed from 15:00 to 16:59 and from 6:00 to 7:00:

RewriteEngine On
# If the hour is 3 PM or 4 PM or 6 AM
RewriteCond %{TIME_HOUR} ^15|16|06$
# Deny all access
RewriteRule ^.*$ - [F,L]