Question

Redirect from domain to subdomain

Posted May 26, 2021 82 views
Nginx

Hello dear community

I have a simple setup: I own two domains and want to point one domain to a subdomain of the other.
So:
1.com
Sub.1.com
2.com

Where 2.com should point to the subdomain sub.1.com. On sub.1.com is running a wordpress in a plesk. I am seeking for real redirect, so that is why I have tried to store this in the nginx.conf.

var/etc/nginx.conf looks like this:

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #tcp_nodelay        on;

    #gzip  on;
    #gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    server_tokens off;

    include /etc/nginx/conf.d/*.conf;


        <^>server {
                listen 80;
                server_name 1.com;
                return 301 $scheme://2.com$request_uri;
        }<^>
}

It seems that the server entry is not sufficient. I am now unsure what else I need to change.

edited by bboucheron

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.

×
Submit an Answer
2 answers

If you have two domain names then I would suggest you that redirect the first domain name to your WordPress subdomain using the DNS you are using. After that use Nginx to redirect example.com to sub.example.com.

Hey, I am sorry. There are missleading information in my question and some information missing.

So this part should look like this:

<^>                server_name 2.com;
                return 301 $scheme://sub.1.com$request_uri;<^>

I have everything on the same server. Therefore I cannot just edit the DNS for the domain. Since both domains are poiting to the same server, I think I need a redirect/server block.

This is very helpful:
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts-3

But since I am running a plesk I dont have the “Sites-availabe” in my nginx or apache directory. I dont think that they are used, so I think this tutorial is not matching my issue.

The sites are stored in /var/vhosts and not in the folders mentioned in the tutorial.

by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.