Question

How do I permanently redirect subdomain to specific URL?

Hi, I’m new to Digital Ocean, and also to nginx.

I have a droplet hosting a couple of different sites, and most traffic for a domain—let’s call it domain.com—should be hosted here. But sub.domain.com needs to redirect to a specific URL, e.g. 12.34.56.78/a/particular/path/.

It seems like DNS only directs to servers, and so I need to create a subdomain-specific redirect.

My file in /etc/nginx/sites-available looks like this:

server {
  server_name domain.com;

  root /var/www/domain/html;
  access_log /var/log/nginx/www.demo.com.access.log;
  error_log /var/log/nginx/www.demo.com.error.log;

  # INCLUDE CONFIG INFO COMMON TO ALL SITES
  include global/common.conf;

  # INCLUDE COMMON WORDPRESS INFO
  include global/wordpress.conf;

  include fastcgi_params;
  fastcgi_param SERVER_NAME $host;
  fastcgi_param DOCUMENT_ROOT root;
}

server {
  server_name  sub.domain.com;
  rewrite ^(.*) http://12.34.56.78/a/particular/path/ permanent;
}

This does not seem to be working, and my browser returns an ERR_NAME_NOT_RESOLVED.

How do I redirect that subdomain traffic to that external address? Note: I want the address in the browser to still say ‘sub.domain.com’.

Thanks anybody for your help!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@eyesofjeremy

If the sub-domain doesn’t need to resolve to the same server as the parent domain, you really don’t need to setup a server block or redirect; it can be handled by a DNS entry. You can do this by simply adding an A entry to your DNS zone; one that points the sub-domain to the alternate IP.

It’d look something like:

A			sub				123.45.678.90

If you need support for www on the sub-domain too:

A			sub				123.45.678.90
CNAME		www.sub			sub.domain.com.

This will keep the sub-domain in the browser as well. An internal redirect, such as what you have above, is just that, a redirect, which means that when X is accessed, the server block is accessed, which forces the server to processed the request, and then redirect it to Y.

With a DNS entry, you alleviate the need for the server to process anything as the DNS zone processes the request for you.

Hi, add this code into subdomain server block location / { return 301 https://www.crazytut.com$request_uri; } Remember change your domain you want to redirect.