AWS Domain Routing

December 2015 · 3 minute read

The following article will go through redirecting a domain on a privately hosted server with AWS. And some of the things to be aware of before you go and do it! This particular example uses a website build with WordPress but is relevant to all web apps.

1. Log into your AWS account and redirect the name servers

In your ec2 hosted zone, set up a new type A record set for the name server you’d like to redirect, give it an appropriate subdomain and point it at the IP address of your server. In this case, selenasmall.com will be redirecting to subdomain www.selenasmall.com

Now you can change the values of the original name server to be pointing at ALIAS www.selenasmall.com with a simple routing policy.

2. In the command line, ssh into your ubuntu server

$ ssh selena

3. Find the .config file for the site you’re redirecting

If you’re unsure where to find it, it’s most likely to be located in the /etc dir for your site which usually stores config files, so you can run a search using the following command which will recursively search for case-insensitive text in files containing the term ‘vhosts’

$ sudo grep -R -i ‘vhosts’ /etc/*

4. Once you find the file, open it and edit the redirects to match up to your ec2 instance

$ sudo nano /etc/apache2/sites-available/blog.selenasmall.com.conf

Original 

Updated

Save and exit the file

Restart the server

$ sudo service apache2 restart

5. Log in to your database in your server and update the ‘siteurl’ file

If you’re unsure where the mysql database is located, you can always search history for it.

$ history | grep mysql

Log in to the database

$ mysql -u root wordpress -p

Find the site’s domain name as referenced in the database

$ select * from wp_options where option_name = ‘siteurl’;

Update the domain name

$ update wp_options set option_value = ‘http://www.selenasmall.com' where option_name = ‘siteurl’;

Check that it was correctly updated > $ select * from wp_options where option_name = ‘siteurl’;

6. Now that you’ve successfully re-routed your domain to another namespace, you’ll notice that your previous links and images are all broken. This is because the path has changed. If you’ve got a big application with a lot of links and images, that could mean a massive amount of work to try and go through to correct all the mistake.

You’ll probably want an easy function or plugin to go through the app and perform a find&replace of all references to the old domain. In the case of WordPress, the Better Search Replace plugin does the trick. https://wordpress.org/plugins/better-search-replace/