D
D
Dmitry Isaev2019-01-22 10:33:05
Nginx
Dmitry Isaev, 2019-01-22 10:33:05

How to set up a redirect in Nginx from site.ru/index.php/slug/ to site.ru/slug/?

Good afternoon, the site was not indexed correctly due to Laravel and the Nginx config not configured.
Now the config looks like this:

server {
  server_name test.ru www.test.ru;
  ssl_certificate "/var/www/httpd-cert/www-root/test.ru_le1.crt";
  ssl_certificate_key "/var/www/httpd-cert/www-root/test.ru_le1.key";
  ssl_ciphers EECDH:+AES256:-3DES:RSA+AES:!NULL:!RC4;
  ssl_prefer_server_ciphers on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  add_header Strict-Transport-Security "max-age=31536000;";
  ssl_dhparam /etc/ssl/certs/dhparam4096.pem;
  charset UTF-8;
  index index.php index.html;
  disable_symlinks if_not_owner from=$root_path;
  include /etc/nginx/vhosts-includes/*.conf;
  include /etc/nginx/vhosts-resources/test.ru/*.conf;
  access_log /var/www/httpd-logs/test.ru.access.log;
  error_log /var/www/httpd-logs/test.ru.error.log notice;
  ssi on;
  set $root_path /var/www/www-root/data/www/test.ru/public;
  root $root_path;
   if ($host ~* www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*)$ https://$host_without_www$1 permanent;
    }

    if ($request_uri ~ "^(.*)index\.(?:php|html)") {
     return 301 $1;
    }
    if (!-f $request_filename) {
        rewrite [^/]$ $uri/ permanent;
    }
  listen 127.0.0.1:443 ssl http2;
  location / {
    try_files $uri $uri/ /index.php?$query_string;
    location ~ [^/]\.ph(p\d*|tml)$ {
      try_files /does_not_exists @php;
    }
    auth_basic "Access limited by ISPmanager";
    auth_basic_user_file /var/www/www-root/data/etc/access.test.ru.1B2M2Y8A.passwd;
  }
  location @php {
    fastcgi_index index.php;
    fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@test.ru";
    fastcgi_pass unix:/var/www/php-fpm/www-root.sock;
    fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
    try_files $uri =404;
    include fastcgi_params;
  }
}

This redirect:
if ($request_uri ~ "^(.*)index\.(?:php|html)") {
     return 301 $1;
    }

removes only index.php
need 301 redirect to remove after site.ru index.php
like site.ru/index.php/asdasda → site.ru/asdasda/
site.ru/index.php/sasdad2 → site.ru/sasdad2/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vader666, 2019-01-22
@IsaevDimka

if ($request_uri ~ "^(.*)index\.(?:php|html)/(.*)") {
     return 301 $1$2;
    }

I
ivankomolin, 2019-01-22
@ivankomolin

location ~^ /index.php/slug {
    return 301 $host;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question