如何仅针对Nginx Server删除html php文件的html php扩展名和url末尾的斜杠

时间:2019-04-23 04:25:58

标签: nginx nginx-location

我想从url中删除html和php扩展名,然后再添加斜杠。到目前为止,我已经删除了html和php扩展名。 但是,当我尝试访问index.php时出现问题。它不会自动打开,并且显示错误“ ERR_TOO_MANY_REDIRECTS”。 但是,如果我手动键入index.php,则它会打开索引文件。另一方面,index.html将自动打开。

当我尝试遵循准则Add slash to the end of every url (need rewrite rule for nginx)时,却出现404错误


这是我当前的服务器块

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/ [如果index.php是索引文件]
www.test.example.com/index.php
www.test.example.com/home/index.html
www.test.example.com/home/about.html

预期结果:

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

当前结果:

错误-重定向过多
www.test.example.com/index.php或www.test.example.com/index
www.test.example.com/home
www.test.example.com/home/about

0 个答案:

没有答案