How to create 301 redirect to a new domain

I am editing nginx.conf. I want to redirect all pages of domain1.com to domain2.com.

How to edit the file to make it work.

I see a line:

if($http_host != domain1.com){
   rewrite (.*) https://domain1.com$1 permanent;
}

if I change it to ‘=’ in the if statement and rewrite to the new domain, will it work or I need to make sure other things to change.

Example:

if($http_host = domain1.com){
   reqrite (.*) https://domain2.com$1 permanent;
}
1 Like

In general, I configure it like so:

server {
    listen 80;
    server_name domain1.com;
    return 301 https://domain2.com$request_uri;
}

The above directive can be ammended in a lot of ways to better suit your needs.

1 Like

Thank you, I have a few questions.

I have the following lines in the config file:

server {
   listen 80
    return 301 https://domain1.com$request_uri
}

I don’t know why 301 it’s there by default. Should I override that or create a new one above or below it (what you provided). Is server_name necessary?

Will that break the LetsEncrypt renewal process if I redirect everything?

1 Like

Are you editing nginx.conf inside the discourse container or an external nginx reverse proxy?

If it is internal, you should instead be doing it in the app.yml file. There are multiple examples on how to do that

If you are doing it on an external nginx rp then you can create a new file say redirect.conf in the /etc/nginx/conf.d folder with the configuration example I provided.

2 Likes

inside the container. I will search for app.yml solutions. I saw one for folder, hope I find one for domain change. Thank you.

1 Like

Did you try this?

3 Likes

Yes, will implement it shortly. Thank you so much for your time.

1 Like

@itsbhanusharma I am implementing the code right now. My question is whether LetsEncrypt will renew itself as I intend to live the old server for some time until the URLs are changed by Google. Is there anything I need to add so LetsEncrypt renewal won’t fail due to this 301 redirection change?

Is this can work Setting up Let’s Encrypt with Multiple Domains

I want to exclude .well-known/acme-challenge from a redirect,

issue with 301 redirect and LetsEncrypt

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.