O
O
Olga2019-04-15 20:49:25
Nginx
Olga, 2019-04-15 20:49:25

How to properly set up a 301 redirect in Nginx when redesigning a site?

Task
There is an old site - mysite.ru (main mirror), other mirrors:
a) mysite.com - there is no physical site on the domain, it is needed to "close" the domain in the .com zone;
b) versions from www.
As a result of the redesign and the jamb, a new site got into production - new.mysite.ru It is
required to glue the old and new sites together.
We do not save the structure of the old site. That is, only the server_name will change
Solution: We place
all the files of the new site in the folder of the old site. We have two identical sites. For gluing, we make a 301 redirect from the new site to the old one.
I found a recommendation https://toster.ru/q/443666 (given over a year ago) - create a separate server block for each domain/subdomain (see below).
1. Is the recommendation relevant? In the documentation for Nginx, versions with and without www are usually located in the same server block, but here everything is decomposed into different blocks. Does the order of server blocks matter in this case?
2. Is this a page redirect? I read that to implement such a redirect, you need to make a list of redirects (from which page of one site to which page of another site the redirect is going on). How is it implemented and in what cases?
3. If the domains were with different protocols (http/https), would it be necessary to take this into account or does it not matter (when redirecting, the target protocol is explicitly specified)?
Option "All domains in one block"
server {
listen 443 ssl;
root /somepath;
server_name mysite.ru www.mysite.ru mysite.com www.mysite.com new.mysite.ru www.new.mysite.ru;
index index.html;
error_page 404 /ErrorPages/404.html;
try_files $uri $uri/ =404;
}
server {
listen 80;
server_name mysite.ru www.mysite.ru mysite.com www.mysite.com new.mysite.ru www.new.mysite.ru;
return 301 https://$server_name$request_uri;
}
Option "by recommendation"
server {
server_name new.mysite.ru;
return 301 https://mysite.ru$request_uri;
}
server {
server_name www.new.mysite.ru;
return 301https://mysite.ru$request_uri;
}
server {
server_name mysite.com;
return 301 https://mysite.ru$request_uri;
}
server {
server_name www.mysite.com;
return 301 https://mysite.ru$request_uri;
}
server {
server_name www.mysite.ru;
return 301 https://mysite.ru$request_uri;
}
server {
listen 443 ssl;
root /somepath;
server_name mysite.ru;
index index.html;
error_page 404 /ErrorPages/404.html;
try_files $uri $uri/ =404;
}
server {
listen 80;
server_name mysite.ru;
return 301 https://mysite.ru$request_uri;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question