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.
- Login to your server or cloud.
- 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.
- Find “.htaccess” file.
In my case “.htaccess” file is located in “/var/www/example.com/public”.
- Now Open this file in your favorite text editor.
- Past this code to the file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ENTER_YOUR_SERVER_IP
RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]
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)
- Now save the file.
- Now restart the Apache server.
Search how to restart apache in your OS.
- Type “sudo service apache2 restart” and hit enter.
This commend will work for, Linux with apache2.
Redirect IP to domain in Nginx.
Follow this step-by-step guide, in order to redirect it to the domain in apache.
- Login to your server or cloud.
- 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.
- Find “nginx.conf” file.
- (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
- Now Open this file in your favorite text editor.
- Past this code to the file.
server {
Server_name ENTER_YOUR_SERVER_IP;
return 301 http://yourdomain.com;
}
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)
- Now save the file.
- Now restart the Nginx.
Search how to restart nginx in your OS.
- 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.
Let me know on Twitter If you feel this post is not helpful for you.