A
A
Alexander Lebedev2017-03-11 19:13:56
Nginx
Alexander Lebedev, 2017-03-11 19:13:56

How to set up a 301 redirect in nginx?

Good afternoon. I switched the site to https and nginx, you need to set up redirects from old addresses to new ones (page urls have also changed).
Actually, as an example: http://realebedev.ru/msc - if you follow this link, it will transfer to the main one, everything is as it should, but if you follow the https link - https://realebedev.ru/msc - it gives 404, although, logically, it should also display on the main page.
Here is a piece of the config on the topic:

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

In my understanding, it should translate both the http and the https version, but it does not translate the second one. Full config in attachment.
What am I doing wrong? :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
ky0, 2017-03-12
@ky0

I would add something like this instead of your sheet:

location / {
       rewrite ^/(.*) https://$host/$1;
}

A
Alexander Lebedev, 2017-03-11
@sortarage

Here is the whole config, just in case:

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;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question