How to redirect IP address and EC2 address to domain

The Why?

We observed our ec2 instance address and IP address of the server were indexed for certain pages and blogs in google searches or any other search engines which was a serious concern with respect to SEO.

The Solution

We needed a solution to redirect and resolve to our original domain (https://readosapien.com).

It may come to your mind that we could do the following by just adding a DNS record, but that won’t work as this is controlled on the managed server side.

The final solution we got from n-google searches was that we need to add a redirect engine entry in config files.

It was a pain to identify which config file to add to, from our research we tried adding the expected changes in httpd.conf file located at /opt/bitnami/apache2/conf/bitnami
But this was not the file that could save us.

TLDR;

We did some more google-searches and suspected that wordpress-htaccess.conf located at /opt/bitnami/apache2/conf/vhosts/htaccess is the file we were looking for.

We added the below lines of code and Hurrah!

RewriteEngine on
RewriteCond %{HTTP_HOST} 111\.111\.111\.111
RewriteRule (.*) https://readosapien.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^ec2-111-111-111-11\.region\.compute\.amazonaws\.com
RewriteRule (.*) https://readosapien.com/$1 [R=301,L]

But don’t be dumb and copy the above lines exactly. Replace it with your IP address and ec3 instance URL.
For more details you cane check out this StackOverflow link.

Restart apache using the command
sudo /opt/bitnami/ctlscript.sh restart apache

Another approach would be specifying the canonical URL. So if your site is accessible using multiple URLs(in our case it was the website URL, ec2 URL, and the IP address), Google will see this as copies of the same page. It’ll pick a URL as a canonical URL and crawl it, whereas others would be considered duplicates.

How to specify a canonical page

  1. Add a link tag pointing to the canonical URL with rel=canonical in all the duplicate pages. A major disadvantage of the approach is that it increases the size of the page
  2. Attach a rel=canonical header in the page’s response
  3. List all your canonical URLs in your sitemap
  4. Use 301 redirects (which is what we did in our approach), which tells google that the redirected URL is a canonical URL

Leave a Comment