redirect ip address to domain

How to Redirect IP to Domain URL using .htaccess in Apache

Sometimes you may need to redirect IP address of your website to its domain URL. You can do this easily in Apache server using mod_rewrite module. It allows you to easily setup URL redirection and rewrites for your website. Here are the steps to redirect IP to domain URL using .htaccess in Apache web server.

How to Redirect IP to Domain URL using .htaccess in Apache

We will be using mod_rewrite to redirect IP address to domain URL. It is a very powerful and commonly used Apache module that allows you to easily rewrite URLs or redirect them based on specific conditions. For our purpose, we will simply check the http host value of incoming requests for IP address, and redirect matching requests to our domain URL.

1. Enable mod_rewrite (.htacces)

Here are the steps to enable mod_rewrite (.htaccess) according to your Linux system.


Open terminal and run the following command to enable mod_rewrite.

$ sudo a2enmod rewrite


Open Apache configuration file in a text editor.

$ sudo vi /etc/apache2/httpd.conf
$ sudo vi /etc/httpd/httpd.conf

Look for the following line.

#LoadModule rewrite_module modules/

Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.

Also look for the following Directory tag and change AllowOverride from None to All.

. . .
<Directory /var/www/html>
. . .
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
AllowOverride All
. . .
. . .

2. Open .htaccess file

Create/Open .htaccess file at the root location of your website /var/www/html. If your website’s document root is located somewhere else, then update the file path below accordingly.

$ sudo vi /var/www/html/.htaccess

3. Redirect IP address to domain

Let us say your webserver’s IP address is and your website’s domain is then add the following lines to your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^45\.43\.42\.41$
RewriteRule ^(.*)$$1 [L,R=301]

In the above code, Apache checks the HTTP_HOST value of each request. If it is then it redirects those URLs to along with rest of request query string. Also, we use R=301 flag in our RewriteRule to ensure permanent 301 redirection, and not a temporary one. Please update the domain name above as per your requirement. You may also change http to https if you want to redirect to HTTPS version of your domain.

4. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo service apache2 restart

That’s it. As you can see, it is very easy to redirect IP address to domain URL in Apache server. In most cases, if you use platforms like WordPress or Drupal, it will contain rewrite rules to redirect IP address to domain.

Also read:

How to Fix Mixed Content/Insecure Content in Apache
How to Force HTTPS in .htaccess in Apache
How to Set Content-Disposition Header
How to Get Package Details in Ubuntu
How to Force Download in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *