Nginx 301 перенаправить со старого сайта на новостной сайт - PullRequest
0 голосов
/ 09 февраля 2019

Ниже URL и их код статуса заголовка.Обратите внимание, что перенаправление происходит.Но в некоторых случаях я вижу 301 в заголовке, а в некоторых случаях не вижу.

https://www.oldsite.com -> 301 found in header
https://oldsite.com -> 301 found in header
http://www.oldsite.com -> No 301 found in header
http://oldsite.com -> No 301 found in header

https://www.newsite.com - Target site
https://newsite.com -> 302 found in header
http://www.newsite.com -> No 301 found in header
http://newsite.com -> No 301 found in header

У меня есть четыре конфигурации, указанные ниже.Есть ли что-то не так с любой из этих конфигураций.Обратите внимание, что это сайт magento.

oldsite.com.nginx.conf

server {
    listen      ipaddress:80;
    server_name oldsite.com www.oldsite.com;
    root        /home/oldsite/web/oldsite.com/public_html;
    index       index.php index.html index.htm;

location / {
 return 301 https://www.newsite.com$request_uri; 
}
    include     /home/oldsite/conf/web/nginx.oldsite.com.conf*;
}

oldsite.com.nginx.ssl.conf

server {
    listen      ipaddress:443;
    server_name oldsite.com www.oldsite.com;
    root        /home/oldsite/web/oldsite.com/public_html;
    index       index.php index.html index.htm;

    ssl         on;
    ssl_certificate      /home/oldsite/conf/web/ssl.oldsite.com.pem;
    ssl_certificate_key  /home/oldsite/conf/web/ssl.oldsite.com.key;

location / {
 return 301 https://www.newsite.com$request_uri; 
}

новостной сайт.com.nginx.conf

server {
    listen      ipaddress:80;
return 301 https://www.newsite.com$request_uri; 
    server_name newsite.com www.newsite.com;

    root        /home/newsite/web/newsite.com/public_html/pub;
    index       index.php;
    autoindex   off;
    charset     UTF-8;
    error_page  404 403 = /errors/404.php;
    add_header  "X-UA-Compatible" "IE=Edge";

}

newsite.com.nginx.ssl.conf

server {
    listen      ipaddress:443 http2;
    server_name newsite.com www.newsite.com;

    root        /home/newsite/web/newsite.com/public_html/pub;
    index       index.php;
    autoindex   off;
    charset     UTF-8;
    error_page  404 403 = /errors/404.php;
    add_header  "X-UA-Compatible" "IE=Edge";

    ssl         on;
    ssl_certificate      /home/newsite/conf/web/ssl.newsite.com.pem;
    ssl_certificate_key  /home/newsite/conf/web/ssl.newsite.com.key;

}

1 Ответ

0 голосов
/ 09 февраля 2019

Чтобы обрабатывать example.com и www.example.com по-разному, вы должны разделить существующий блок server на два и поместить требуемый оператор return в один из них.

Например:

server {
    listen      443 ssl http2;
    server_name example.com;

    ssl_certificate      /home/newsite/conf/web/ssl.newsite.com.pem;
    ssl_certificate_key  /home/newsite/conf/web/ssl.newsite.com.key;

    return 301 https://www.newsite.com$request_uri;
}

server {
    listen      443 ssl http2;
    server_name www.example.com;

    ssl_certificate      /home/newsite/conf/web/ssl.newsite.com.pem;
    ssl_certificate_key  /home/newsite/conf/web/ssl.newsite.com.key;

    root        /home/newsite/web/newsite.com/public_html/pub;
    index       index.php;
    autoindex   off;
    charset     UTF-8;
    error_page  404 403 = /errors/404.php;
    add_header  "X-UA-Compatible" "IE=Edge";

    ...
    ...
    ...
}
...