How do I set my domain to forward to a subdomain?

June 29, 2016 513 views

Please view my current DNS set up: link to set-up

I want to have subdomain: and I want all requests to to be forwarded to

The result being whether you provided the blog subdomain or not you are taken to the so the user knows that they are viewing the blog.

I have tried creating an A record and a CNAME record. Neither work.

For a CNAME if I set the name "@" and the hostname "blog." I get the error: cname records cannot share a name with other records.

Do I need a differently configured CNAME record or an A record?


2 Answers

Did you just want to redirect all http traffic to One method to do this is to create a catch-all DNS entry and then create a virtual host (or server block) with your webserver. For DNS

CNAME    *

Note the period at the end above when entering the record in the DO control panel.

Search the tutorials to match your distribution and webserver; search for the string "virtual host". In your case, set the default virtual host or server block to handle

  • Hi gndo,

    Thanks for your response.
    Ideally I'd have forward to Soon I plan to add more subdomains which I would want to work independently.

    If this isn't possible I'll just have point to the same IP as

If you want requests for other domains to be redirected to (i.e. the user always sees '' in their url bar), then you need to set up a 301 redirect. This is accomplished via your web server. Let me know what server you are using, and I'll show you how.

  • Hi rdkalser,

    That'd be great. Thanks.

    I'm using docker, running a wordpress container as well as a mariaDb and phpmyadmin containers. Set up by following this post:

    I imagine i'll need another container running a web server?

    This article provides a real-world example of using Docker Compose to install an application, in this case WordPress with PHPMyAdmin as an extra. WordPress normally runs on a LAMP stack, which means Linux, Apache, MySQL/MariaDB, and PHP. The official WordPress Docker image includes Apache and PHP for us, so the only part we have to worry about is MariaDB.
    • Sure, you could add another container. Personally, however, I would just install nginx directly on the droplet, have it redirect and/or proxy requests for *.mydomain.tld on port 80 to the wordpress container listening on some other upstream_port on the loopback interface.

      Step 1 - Install Nginx.
      • apt-get update && apt-get install nginx
      Step 2 - Disable the default site.
      • rm /etc/nginx/sites-enabled/*
      • rm /etc/nginx/conf.d/*
      Step 3 - Create a new virutual host.
      • nano /etc/nginx/sites-available/mydomain_proxy
      server {
          listen 80 default_server;
          return 444;
      server {
          listen 80;
          server_name *.mydomain.tld mydomain.tld;
          return 301 http://blog.mydomain.tld$request_uri;
      server {
          listen 80;
          server_name blog.mydomain.tld;
          location / {
              proxy_redirect     off;
              proxy_set_header   Host $host;
              proxy_set_header   X-Real-IP $remote_addr;
              proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header   X-Forwarded-Host $server_name;
      Step 4 - Enable the new site.
      • ln -s /etc/nginx/sites-available/mydomain_proxy /etc/nginx/sites-enabled
      Step 5 - Restart nginx
      • service nginx restart
      Step 6 - Modify DNS records.

      Add a CNAME record for host * that points to any host.mydomain.tld. for which there is an A record corresponding to the nginx server's public IP address.

      Step 7 - Open port 80

      If you haven't done so already, allow trafic to port 80 from the world. Use UFW for simplicity.

      • ufw enable && ufw allow 80/tcp

      Now any request for hosts other than blog.mydomain.tld will redirect to blog.mydomain.tld. If you must use a container, do the exact same thing, and be sure to link the appropriate ports.

Have another answer? Share your knowledge.