V
V
Volton2017-08-02 13:37:26
Nginx
Volton, 2017-08-02 13:37:26

How to make a 301 redirect in Nginx one page to the main page?

The site works on https. Redirects from www to non-www are configured.
There is one external old link leading to the site, such as site.com/index.htm and I want to redirect it to the main https://site.com
Please tell me how to do this. I have already exhausted all the options ... I give an example of a config:

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

server {
    listen      443 ssl;
    server_name www.site.com;

    ssl_certificate     /etc/nginx/ssl/site_com/sitecom-ssl-bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/site_com/site_com.key;

    return      301 https://site.com$request_uri;
}

server {
    listen      443 ssl;
    server_name site.com;

    ssl_certificate     /etc/nginx/ssl/site_com/sitecom-ssl-bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/site_com/site_com.key;
    # other settings ssl

    root /var/www/site.com;
    index index.html;

    location /robots.txt {
        alias /var/www/site.com/public/robots.txt;
        access_log off; log_not_found off;
    }

    location / {
        try_files $uri $uri/ =404;
    }
}

Tried:
if ( $request_filename ~ index.htm ) {
        rewrite ^ http://site.com? permanent;
    }

rewrite ^/index.htm$ https://site.com$1 permanent;
and other designs, but did not work(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-08-02
@Volton

didn't you try that?

location = /index.html {
    return 301 https://site.com
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question