I want to remove the html and php extension from url and then I want to add trailing slash. So far I have removed the html and php extension. However, there is a problem when I am trying to access index.php. It does not open automatically and it shows error "ERR_TOO_MANY_REDIRECTS". But if I manually type index.php then it does open the index file. On the other-side, index.html opens automatically.

When I am trying to follow guideline Add slash to the end of every url (need rewrite rule for nginx) , but I am getting 404 error


This is my current server block

server {
    server_name test.example.com www.test.example.com;
    root /var/www/html/test.example.com/;

    include /etc/nginx/ssl/test.example.com.conf;

    index index.php index.html index.htm index.nginx-debian.html;

    error_log /var/log/nginx/test_example_com.log;

    location / {

        rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
        rewrite ^/(.*)/$ /$1 permanent;

        try_files $uri/index.html $uri.html $uri/ $uri @extensionless-php;

    }


    location ~ \.php$ {
            try_files $uri $fastcgi_script_name =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;

            fastcgi_index index.php;
            include fastcgi.conf;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

}

URL

www.test.example.com/ [if index.php is the index file]
www.test.example.com/index.php
www.test.example.com/home/index.html
www.test.example.com/home/about.html

Expected result:

www.test.example.com/
www.test.example.com/
www.test.example.com/home/
www.test.example.com/home/about/

Current Result :

Error - too many redirect
www.test.example.com/index.php or www.test.example.com/index
www.test.example.com/home
www.test.example.com/home/about

0 Answers