Skip to main content

Title: Redirecting All Users to HTTPS, Except for Specific IPs Using .htaccess

In web development, sometimes you may want to redirect all users accessing your website to a specific URL, such as However, there might be certain cases where you need to exclude specific IP addresses from this redirection. In this article, we will explore how to achieve this using the .htaccess file.

Step 1: Accessing the .htaccess File:
The .htaccess file is a configuration file used by Apache web servers to handle various settings. To redirect users and exclude specific IP addresses, you need to locate and edit this file. The .htaccess file is usually found in the root directory of your website. If it doesn't exist, you can create a new file and name it ".htaccess."

Step 2: Adding the RewriteEngine Directive:
To begin, open the .htaccess file and ensure that the RewriteEngine directive is enabled. Add the following line if it's not already present:

RewriteEngine On

Step 3: Writing the RewriteCond Rules:
Now, we will specify the conditions under which the redirection will occur. In this case, we want to redirect all users except for two specific IP addresses: and To achieve this, add the following lines to your .htaccess file:

RewriteCond %{REMOTE_ADDR} !^1\.1\.1\.1$
RewriteCond %{REMOTE_ADDR} !^5\.5\.5\.5$

These lines use the RewriteCond directive to define the conditions. The %{REMOTE_ADDR} variable represents the IP address of the user accessing the website. The exclamation mark (!) indicates negation, so the conditions will be true for all IP addresses except for and

Step 4: Writing the RewriteRule:
Now, we will define the RewriteRule that performs the actual redirection. Add the following line to your .htaccess file:


RewriteRule ^(.*)$$1 [R=301,L]

This RewriteRule will redirect all incoming requests to while preserving the requested URL path (e.g., will redirect to The [R=301,L] flags indicate a 301 redirect (permanent redirect) and stop further rule processing.

Step 5: Save and Test:
After completing the previous steps, save the .htaccess file and upload it to the root directory of your website. Make sure the file permissions are correctly set.

To verify if the redirection works as intended, try accessing your website from different IP addresses. You should be redirected to unless your IP address matches or, in which case you will remain on the original page.

Redirecting all users to a specific URL while excluding certain IP addresses can be accomplished using the .htaccess file. By following the steps outlined in this article, you can easily redirect all users to, except for the IP addresses and This technique provides flexibility and control over the redirection process, ensuring that specific users or IPs are exempted from the redirection rule.

full code

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^76\.67\.28\.238$
RewriteCond %{REMOTE_ADDR} !^88\.84\.28\.106$
RewriteRule ^(.*)$$1 [L,R=301]