How do I create a domain redirect in App Platform?

App Platform does not support 301 or 302 responses to direct traffic from one domain to another, but you can create a free blank app with a custom domain that redirects the app’s traffic to another app’s domain.

To do this, download the following spec to your local machine and save it as redirect.yaml.

    
        
            
alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
domains:
- domain: www.example.com
  type: PRIMARY
features:
- buildpack-stack=ubuntu-22
ingress:
  rules:
  - match:
      path:
        prefix: /
    redirect:
      authority: example.com
      redirect_code: 302
name: domain-redirect
region: lon

        
    

Then update the domain field with the domain you want to direct from and the authority field with the target domain that you want to direct traffic to. In that sample spec, the domain www.example.com redirects to example.com.

Once you have updated the spec file, you can pass the spec to the App Platform using doctl, DigitalOcean’s official CLI.

To pass the spec to App Platform using doctl, run the following command from the same directory where you saved the spec file:

doctl apps create --spec redirect.yaml

App Platform creates the app with the redirect.

After the sample app deploys, you need to add a CNAME record to your domain’s DNS configuration that points the redirected domain to the blank app’s starter URL.

For example, if your blank app has the starter URL domain-redirect-vgfra.ondigitalocean.app, and you want to redirect www.example.com to example.com, add a CNAME record that points www.example.com to domain-redirect-vgfra.ondigitalocean.app, like this:

A screenshot of the DNS control panel for example.com. The CNAME tab is selected, the HOSTNAME field's value is www, and the IS AN ALIAS OF field's value is domain-redirect-vgfra.ondigitalocean.app.

Once you’ve added the CNAME record, the domain redirects to the target domain. It may take several minutes for the DNS changes to propagate across the internet.

Ensure your Dockerfile’s path is correctly defined using the dockerfile_path parameter in your app spec file.