Although most domain registrars or standalone DNS services have a website redirecting service, the setup is always a little different. Examples:
- Cloudflare: Page Rules, where free accounts are limited to 3 page rules.
- Google Domains: Website section where you can add forwarding rules.
- AWS: You can use S3 for http redirecting and Cloudfront for https, where you have to also setup your own certificate. This is extremely tedious. And then you have to set your ANAME or CNAME records to S3 or Cloudfront endpoints.
- Other Premium DNS Services: Synthetic DNS settings like URLFWD or WEBFWD records. These are not even DNS settings but they let you use the same interface to interact with their web redirecting service in the zone settings.
Furthermore, not every redirecting service has the same capabilities. Some can forward https requests, while others can’t, depending on whether they want to handle the SSL certificates or not.
When you change DNS providers, you’ll generally have to setup your own website redirecting all over again. And it might even limit which DNS provider you can go with, because web redirecting isn’t even a DNS capability.
So I decided to use one of my Oracle Always Free Compute Instances as a web server, primarily focusing on web redirecting. Then I can set all A records on my apex and www subdomain CNAME records to the IP address of my compute instance and have a consistent place to do all my web redirection.
First, let’s get the Oracle Compute Instance ready to handle http and https requests. Log into your Oracle Cloud Console and go to Networking, then Virtual Cloud Networks and click on the virtual network used by your compute instance. Click on the subnet. You should have the following Ingress Settings:
- Stateless: Unselected (this is a stateful rule)
- Source Type: CIDR
- Source CIDR: 0.0.0.0/0
- IP Protocol: TCP
- Source Port Range: All
- Destination Port Range: 443
Repeat and add another ingress rule for Destination Port Range: 80.
My Oracle Compute Instance is running Ubuntu 20.04, and I had to install firewalld
and run the following commands:
sudo apt install firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=443/tcp
sudo firewall-cmd --reload
Then I used Caddy to serve the redirects. A typical entry in the Caddyfile might look like this:
www.somedomain.dev {
redir https://www.somedomain.com{uri} permanent
}
Then set your www CNAME (or A record if you are redirecting an apex domain) record to point to the IP address of your Oracle Cloud Instance and you are good to go!