Redirect IP to domain or URL in Nginx, Apache, AWS, cPanel

I hope you enjoy reading this!
Don’t forget to share this blog with your friends.

Table of Contents
Redirect IP to domain

Redirect IP address to domain or URL is required nowadays for production sites due to some security and SEO issues. In this blog, I am going to teach you every single step for Apache and Nginx servers.

Redirect IP to domain in AWS, DigitalOcen, etc clouds.

People (web owners) ofter think that this issue is solved on the Platform level but it isn’t.

This issue is not caused by AWS, DIgitalOcen, etc clouds, this issue is caused by your server.

So don’t worry this solution work for all platforms.

Test you have this issue or not?

You are here it means you already checked the issue but to make sure by simply enter your IP in Brower if it loads your website without change the URL it means you have this issue.

Why redirecting IP to domain is important?

Redirecting IP to the domain is necessary nowadays for security and SEO issues.

The SEO issue.

If you don’t redirect your server’s IP to a domain google and other search engines think both (your IP, and your Domain) are separate websites and this hurts your ranking.

The security issue.

You might use Cloudflare. (If not I highly recommend)

Have you even notice that after setting up Cloudflare your actual webserver’s IP is not available publicly.

And it’s a good practice to make IP private as much as you can. 

Redirect IP to domain in Apache (.htaccess or cPanel).

Follow this step-by-step guide, in order to redirect it to the domain in apache.

  1. Login to your server or cloud.
AWS Dashboard
  1. Connect to your server because we need to update some files.

If you don’t know how to connect search online you definitely find something great. 

  1. Find “.htaccess” file.

In my case “.htaccess” file is located in “/var/www/example.com/public”.

  1. Now Open this file in your favorite text editor.
  2. Past this code to the file.
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^ENTER_YOUR_SERVER_IP 
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
Past code in .htaccess
Past code in .htaccess

Don’t forget to replace ENTER_YOUR_SERVER_IP with your actual IP address.

Very Important note: Don’t change anything here just copy and post that code. (As shown in image)

  1. Now save the file.
  2. Now restart the Apache server.

Search how to restart apache in your OS.

  1. Type “sudo service apache2 restart” and hit enter.

This commend will work for, Linux with apache2.

Restart the Apache server
Restart the Apache server

Redirect IP to domain in Nginx.

Follow this step-by-step guide, in order to redirect it to the domain in apache.

  1. Login to your server or cloud.
AWS Dashboard
  1. Connect to your server because we need to update some files.

If you don’t know how to connect search online you definitely find something great. 

  1. Find “nginx.conf” file.
  2. (Optional: If you don’t know where niginx.conf is.) Type “nginx -V 2>&1 | grep -o ‘\-\-conf-path=\(.*conf\)’ | cut -d ‘=’ -f2” in order to find “nginx.conf” file.
nginx -V 2>&1 | grep -o '\-\-conf-path=\(.*conf\)' | cut -d '=' -f2
Find “nginx.conf” file
Find “nginx.conf” file
  1. Now Open this file in your favorite text editor.
  2. Past this code to the file.
server { 
    Server_name ENTER_YOUR_SERVER_IP; 
    return 301 http://yourdomain.com; 
}
Past code in nginx.conf
Past code in nginx.conf

Don’t forget to replace ENTER_YOUR_SERVER_IP with your actual IP address.

Very Important note: Don’t change anything here just copy and post that code. (As shown in image)

  1. Now save the file.
  2. Now restart the Nginx.

Search how to restart nginx in your OS.

  1. Type “sudo systemctl restart nginx” and hit enter.

This commend will work for, Ubuntu 18.04 /16.04, CentOS 7/8, and Debian 10 /9 releases.

Restart the Nginx server
Restart the Nginx server

Let me know on Twitter If you feel this post is not helpful for you.

Leave a Comment

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

Get an AMAZING ARTICLE for FREE that cost me 100$
Get an AMAZING ARTICLE for FREE that cost me 100$