Tutorial - How do I block access to my website for certain IP addresses via a .htaccess file?
Find out about the actions you can take via a .htaccess file to block access to your website for certain IP addresses
Objective
The aim of this tutorial is to help you secure access to your websites from external networks, and prevent intrusions and DoS attacks.
You can do this with a ".htaccess" file, a particular text file, that the web server (Apache) detects, and that allows you to define special rules for a directory and all of its subdirectories.
You can create multiple ".htaccess" files in the FTP space of your hosting but only one per directory or subdirectory to avoid conflicts between different .htaccess files.
Find out how to block access to your website for certain IP addresses via a ".htaccess" file.
Information regarding OVHcloud service administration and how to find appropriate assistance
When using OVHcloud guides, please be aware of the following conditions:
- User instructions aim to provide as many details as possible but cannot cover individual use cases. You might need to adapt the pertinent actions to your requirements.
- The OVHcloud ecosystem is built for flexibility and freedom of choice. Customers are therefore responsible for the secure and proper configuration of their services. To prevent data loss, we strongly recommend to apply backup strategies to all your important data.
- Our guides and tutorials may reference third-party software or services in combination with OVHcloud solutions. The technical support provided by OVHcloud does not include the configuration of systems or products outside of our responsibility. This includes but is not limited to:
- Operating systems and user interfaces (Windows, Debian, Plesk, etc.).
- Any other third-party software (FTP clients, email software, etc.).
- Services offered by other providers (DNS, APIs, user interfaces, etc.).
To receive the appropriate assistance for any issues you might experience, follow these guidelines:
- You seek personalized advice or you would like to discuss a topic that is not covered in detail by our documentation?
Join the OVHcloud Community to search for your topic and reach out to other users. - You need to report an incident regarding your OVHcloud service or you are experiencing difficulties in the OVHcloud Control Panel?
Create a support request in our Help Centre. - You require professional assistance for your project or you need help with tasks outside our support scope?
Visit our partner portal to search for experts who are familiar with OVHcloud solutions. - You are looking for more detailed information regarding our support levels and Professional Services?
Please visit our web pages for OVHcloud support levels and OVHcloud Professional Services.
You can participate in improving our documentation:
- You would like to share feedback to improve a guide page or you want to report insufficient information on a specific page?
Use the "Was this page helpful?" buttons at the bottom of the page to let us know. - You would like to propose a specific documentation update?
Use the "Edit this page" function, available at the bottom of the page and in the sidebar.
Requirements
Instructions
The ".htaccess" file can be placed in several different folders, while respecting the rule of only one ".htaccess" file per folder or subfolder.
The settings defined by a ".htaccess" file apply to the directory where it is installed and to all subdirectories.
To edit (or create) these directories, log in to your hosting plan’s FTP space. If you need help with this, please refer to our guide on Logging in to your Web Hosting plan’s storage space.
Block an IP, a range of IPs, a domain or all the IPs of a country
Several rules are available to block access to your hosting plan via ".htaccess".
Be careful with the syntax and block settings to prevent blocking yourself from viewing your hosted sites and/or scripts.
In the event of an error, you can always log in to the FTP space of your hosting to correct mistakes.
Shared hosting currently works with Apache 2.4.
For more details on the syntax described in this guide, consult the following official pages:
Block an IP
To block a specific IP address, insert the following code into your ".htaccess" file:
- Example: If you want to block the IP address 203.0.113.0, you will need to write the following code:
Block an IP range
To block an IP address range, insert the following code into your ".htaccess" file:
- Example: If you want to block all IPs in 203.0.113.x, you will need to write the following code:
Block a domain
Some domains might access your hosting via redirections or requests.
To block a domain, insert the following code into your ".htaccess" file:
- Example: if you want to block domain.tld, you will need to write the following code:
Block IPs from a country
All IP addresses (particularly public IP addresses) have country-wide geolocation. This way, you can get an idea of where an IP's traffic comes from, and physically locate the IP.
The ".htaccess" allows, thanks to this element, to block all the geolocated IPs from a country. In other words, anyone who tries to visit your site from this country will be blocked (unless they use a VPN connection with a geolocated IP in another country).
Blocks via the ".htaccess" are done through the two-letter Country Codes (ISO 3166-1 alpha2 standard) of the countries.
Several websites list the countries and their respective Country Codes, including https://www.iban.com/country-codes (independent of OVHcloud).
To block all IPs of a country, insert the following code at the top of your ".htaccess" file:
- Example: If you want to block geolocated IP addresses from Fiji (FJ) and Greenland (GR), you will need to write the following code at the top of your ".htaccess" file:
To authorise selected IPs, a range of IPs or all the IPs of a country
Rather than restricting access to one or more IPs and allowing others to access your hosting, you can do the opposite by blocking all IPs and then allowing only one or more IPs to access your service.
Authorise one or more IPs
To authorise only one IP to access your service, insert the following code into your ".htaccess" file:
- Example: If you only want to authorise IPs 203.0.113.0 and 203.0.113.1 to access your hosting, you will need to write the following code:
Authorise an IP range
To authorise a range of IPs to access your service, insert the following code at the top of your ".htaccess" file:
- Example: If you only want to authorise the IP range 203.0.113.x to access your hosting, you will need to write the following code at the top of your ".htaccess" file:
Authorise all the IPs of a country
To authorise all IPs in a country to access your service, insert the following code at the top of your ".htaccess" file:
- Example: If you wish to authorise only Fiji (FJ) and Greenland (GR) to access your hosting, you will need to write the following code at the top of your ".htaccess" file:
Further actions with the ".htaccess" file
Besides general access security to the hosting, the ".htaccess" file allows you to perform other actions. Below are more OVHcloud tutorials on the subject:
- Protect your website’s administration interface via ".htaccess".
- Rewrite your URLs using mod_rewrite
- Perform other operations with the ".htaccess" file
Go further
For specialised services (SEO, development, etc.), contact OVHcloud partners.
If you would like assistance using and configuring your OVHcloud solutions, please refer to our support offers.
Join our community of users.