Question

Redirect from domain to subdomain

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.


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.

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.

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.