V
V
Vladimir Bryzgalov2015-12-25 14:44:44
Nginx
Vladimir Bryzgalov, 2015-12-25 14:44:44

How to make 301-redirect in Nginx from index.php to site root(/)?

Good afternoon! I'm moving my website from shared hosting to VDS. The whole problem is that Apache + php-cgi worked on the hosting, and I set up the VDS on Nginx + php-fpm. Accordingly, all rules in .htaccess are ignored.
At the moment, most of the problems with mod_rewrite have been visually solved, but one significant problem remains: it is necessary to implement a 301-Redirect from site.ru/index.php and site.ru/index.html to site.ru/.
With index.html it was possible to solve by introducing the directive:

location /index.html {
    rewrite ^(.*)$ http://site.ru/ permanent;
    }

If you do this with index.php, you get a circular redirect.
Tried several options from the internet. In particular:
if ($request_uri ~* "^(.*/)index\.php") {
        return 301 $1;
  }

The above method works, but some scripts on the site do not work correctly, in particular search.
Are there any other configuration options to implement the correct redirect?
PS on the site with Apache, the following directives were used to implement the redirect mechanism and all scripts on the site worked properly:
##Redirect from index.php to root

RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^index\.php$ http%2://site.ru/ [R=301,L]

##END Redirect from index.php to root

##Redirect from index.html to root

RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^index\.php$ http%2://site.ru/ [R=301,L]

##END Redirect from index.html to root

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Chernykh, 2015-12-25
@sashkets

delete locations with index files and write
index index.php index.html;

V
Vladimir Bryzgalov, 2015-12-25
@VladimirBryzgalov

server {
    listen *:80 default_server;
    server_name localhost;
    root /var/www/web/sites/$host;
    index index.html index.htm index.php;

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

    location ~ .php$ {
##      try_files      $uri =404;
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_ignore_client_abort off;
    }
}

    include phpmyadmin.conf;

    location /catalog.html {
    rewrite ^(.*)$ http://site.ru/ permanent;
    }

    location /index.html {
    rewrite ^(.*)$ http://site.ru/ permanent;
    }


    location ~ /\.ht {
        deny all;
    }

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

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question