D
D
demon510852020-07-21 15:14:51
Nginx
demon51085, 2020-07-21 15:14:51

How to set up nginx 301 redirect from index.php to root /?

How to set up a 301 redirect from index.php to the root / on nginx, otherwise duplicates of the main page appear in the search results in the form of links to index.php, including with all sorts of parameters. There are no such links on my site. Apparently the search robot finds them on other sites. There are many different options on the Internet, but I would like to understand which method is best. Now I wrote this and it works:

index index.php;

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

        if ($request_uri ~ /index.php) {
                return 301          /;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

but I don't like to use if, and it also confuses me that every request goes through this if.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2020-07-21
@ky0

location = /index.php {
*тут ваш редирект*
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question