sortarage
@sortarage
Я тучка-тучка-тучка, я вовсе не медведь

Как правильно настроить 301 редирект в nginx?

День добрый. Перевел сайт на https и nginx, нужно настроить редиректы со старых адресов на новые (урлы страниц тоже поменялись).

Собственно, как пример: http://realebedev.ru/msc - если перейти по эт ссылке, переведет на главную, все как надо, но если перейти по https ссылке - https://realebedev.ru/msc - то выдает 404, хотя, по логике, тоже должно выводить на главную.

Вот кусок конфига на тему:

if ( $request_filename ~ msc ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }

В моем понимании, переводить должно, как хттп, так и хттпс версию, но вторую не переводит. Полный конфиг во вложении.

Что я делаю не так? :)
  • Вопрос задан
  • 823 просмотра
Пригласить эксперта
Ответы на вопрос 2
ky0
@ky0 Куратор тега Nginx
Миллиардер, филантроп, патологический лгун
Я бы вместо вашей простыни добавил что-нибудь подобное:

location / {
       rewrite ^/(.*) https://$host/$1;
}
Ответ написан
sortarage
@sortarage Автор вопроса
Я тучка-тучка-тучка, я вовсе не медведь
Вот весь конфиг, на всякий случай:

server {
     listen  194.67.213.162:80;
     server_name  www.realebedev.ru;
     rewrite ^ https://realebedev.ru$request_uri? permanent; #301 redirect
}
server {
    listen      194.67.213.162:80;
    server_name realebedev.ru;
    root        /home/admin/web/realebedev.ru/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/realebedev.ru.log combined;
    access_log  /var/log/nginx/domains/realebedev.ru.bytes bytes;
    error_log   /var/log/nginx/domains/realebedev.ru.error.log error;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    unix:/var/run/php/realebedev.ru.sock;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/admin/web/realebedev.ru/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   /home/admin/web/realebedev.ru/stats/;
        include /home/admin/web/realebedev.ru/stats/auth.conf*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/admin/conf/web/nginx.realebedev.ru.conf*;


    if ( $request_filename ~ index.html ) {
        rewrite ^ https://realebedev.ru permanent;
    }

    if ( $request_filename ~ contacts.html ) {
        rewrite ^ https://realebedev.ru/kontakty/? permanent;
    }

    if ( $request_filename ~ ipotechnie-kredity.html ) {
        rewrite ^ https://realebedev.ru/uslugi-rieltora-pri-pokupke-kvartiry-v-ipoteku/? permanent;
    }

    if ( $request_filename ~ konsultacii.html ) {
        rewrite ^ https://realebedev.ru/konsultatsiya-rieltora/? permanent;
    }

    if ( $request_filename ~ obmen-kvartir.html ) {
        rewrite ^ https://realebedev.ru/obmen-kvartir-v-moskve-rieltor/? permanent;
    }

    if ( $request_filename ~ pokupka-kvartir.html ) {
        rewrite ^ https://realebedev.ru/rieltor-dlya-pokupki-kvartiry/? permanent;
    }

    if ( $request_filename ~ prodaja-kvartir.html ) {
        rewrite ^ https://realebedev.ru/rieltor-dlya-prodazhi-kvartiry/? permanent;
    }

    if ( $request_filename ~ rieltory.html ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }

    if ( $request_filename ~ source.html ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }

    if ( $request_filename ~ uslugi.html ) {
        rewrite ^ https://realebedev.ru/uslugi-rieltora/? permanent;
    }

    if ( $request_filename ~ vukyp-kvartir.html ) {
        rewrite ^ https://realebedev.ru/srochnyj-vykup-kvartir-v-moskve-rieltor/? permanent;
    }

    if ( $request_filename ~ zarybajnaya-nedvijimost.html ) {
        rewrite ^ https://realebedev.ru/rieltor-po-zarubezhnoj-nedvizhimosti/? permanent;
    }

    if ( $request_filename ~ special ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }

    if ( $request_filename ~ msk ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }
    if ( $request_filename ~ msc ) {
        rewrite ^ https://realebedev.ru/? permanent;
    }

    if ( $request_filename ~ sitemap.xml ) {
        rewrite ^ https://realebedev.ru/sitemap.xml  permanent;
    }

    if ($scheme = http) {
        return 301 https://$server_name$request_uri;
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы