Skip to content

How to Redirect an IP Address to a Domain: Easy-to-Follow Guide

Would you like to learn how to redirect an IP address to a domain? There’s a simple way that you can do it in just a few minutes.

Let’s explore the process so you can start using this technique right away!

📚 Table of contents:


What does it mean to redirect an IP address to a domain?

To redirect an IP address to a domain means to create an automatic mechanism that will reroute visitors who type an IP address to a domain name that corresponds with that IP address.

The IP address is a unique identifier of a device or a website server on a network. Your server has an IP address that you and your visitors can use to connect to it. This IP address is a string of numbers, typically formatted like this: 192.161.1.24.

For humans, connecting to a website by using an IP addresses is not very practical. Imagine if we needed to remember the IP addresses of each site we visit. It could be very difficult, even impossible, to remember all of the numbers.

This is why we have domain names. The domain is a human-readable name that people can easily remember to access a website without the need to know the IP address of the website. There is a special system called Domain Name System (DNS) that links domain names to their IP addresses for convenience.

But in this case, we would like to learn how to manually redirect an IP address to a domain.


Why do you need to redirect an IP address to a domain?

Creating this redirect ensures that all visitors who have typed your IP address instead of your domain automatically land on your domain/website and can continue smoothly from there.

In practice, there will be an extremely low number of such visitors to your site, but it’s still worthwhile to redirect them – just in case.


How to redirect an IP address to a domain

There are two types of redirects – 301 permanent and 302 temporary. You can learn more about them in our article about domain forwarding. In our case, we will use 301 permanent redirects.

How to redirect an IP address to a domain name will depend on the type of server you use. We are going to explore two of the most popular options – Apache servers and NGINX servers.

NGINX and APACHE logos.

Apache IP address to URL redirect

1. Log in to the server using your admin credential. You can do that by typing the following command in the Terminal application (Linux and macOS users) or in the Command Prompt on Windows:

ssh [username]@[host_ip_address]

Replace “username” with your username, and “host_ip_address” with the IP address of your server. After that, the computer will ask you to enter your password. Do it and press Enter.

2. Next, you’ll to edit the configuration file. To access it, go to the Vim Editor through the Terminal/Command Prompt. Note that for this tutorial, we chose the Vim Editor because it’s very popular and built-in on most operating systems. Once inside the editor, type the following command:

vim /etc/apache2site-available/000-default.conf

You’ll also need to add a directory, so you can allow override and get htaccess capability. To do it, first locate the “DocumentRoot /var/www/html”.

Under it, add:

<Directory /var/www/html>
AllowOverride ALL
</Directory>

Use the “:wq” to write the changes and save it.

3. Next, go into the htaccess file. Inside the Terminal/Command Prompt, type:

vim /var/www/html/.htaccess

Inside the htaccess file, you’ll need to add a few lines:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4$
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L, R=301]

When you are ready, use the “:wq” command and press Enter to save.

4. After that enable the Rewrite module with the following command:

a2enmod rewrite

The computer will prompt you with a message that you need to restart the system. Do this by using the following command:

systemctl restart apache2

Now you are ready for testing. We’ll cover that step later, but if you don’t care for the NGINX portion of this tutorial, then you can skip to it now.

Example of Apache server configuration to redirect an IP address to a domain:

Apache configuration for redirecting an IP address to a domain.

Example of htaccess:

Htaccess example.

NGINX IP address to URL redirect

1. First, log in to your server using your admin credential. You can do that by using the following command in the Terminal application (Linux and macOS) or Command Prompt (Windows):

ssh [username]@[host_ip_address]

Replace “username” with your username, and “host_ip_address” with the IP address of your server. After that, you need to type your password and press Enter.

2. Next, you’ll need to edit the configuration file. To access it, type:

vim /etc/nginx/sites-available/default

This one is the default option. If you’ve changed it, use the one you have created before. Note that we once again used the Vim Editor here. If you prefer a different editor, you can change it in the command line.

3. Add another server block with the following content:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 1.2.3.4;
return 301 https://www.yoursite.com$request_uri;
}

The server will listen on port 80. Server_name is the IP address of your server. In the example, it is “1.2.3.4”.

Replace it with your server’s IP address. After “return 301” (permanent redirect) you’ll see the URL this redirect will lead to – “https://www.yoursite.com”.

Change it to your site’s URL.

When you are ready you can save the changes with the “:wq” command. It will write the changes and quit.

4. The only thing left to do is to restart the server. You can do it with the following command:

systemctl restart nginx

Then press Enter. Your redirect is ready and you can test it.

Example of an NGINX configuration to redirect an IP address to a URL:

NGINX configuration to redirect an IP address to a RUL.

Testing your redirect

Testing a redirect from an IP address to a domain name is quite easy:

  1. Open your browser of choice.
  2. Type the IP address of your server. If your site has multiple IP addresses (like an IPv4 and an IPv6), make sure to type in the same one that you used in the configuration.

If the redirect works correctly, the server will redirect your request from the IP address to the domain name and you’ll see the domain name of your site written in the address bar of your browser. If that is not the case, follow the troubleshooting process below.


Troubleshooting common issues

Internal server error

You can get this message if you have some spelling errors. Please check and if you find some, correct them and try again.

Nothing happens

If nothing happens, and you don’t get an error, you might have an error with the IP address you have used for the configuration. Check the configuration and correct the IP address. This should make the redirect work.

Redirecting to the wrong URL

Check if you have input the correct URL. Often people forget to write “https://” before the domain name inside the configuration and this will lead to a bad redirect. Check for errors, correct them and the redirect should work.


Conclusion 🧐

Now that you’ve finished reading this tutorial, you should know how to redirect an IP address to a domain name in just a few minutes. If you do it correctly, then in the future, if any visitors type your IP address into their browser, they will automatically get redirected to the domain name you set up for the redirect.

If you want to learn more about how redirects can be useful to your website, check out our guide to URL masking.