nginx - 隐藏 .php 扩展名,并将 *.php 请求永久重定向到干净的 URL

nginx - 隐藏 .php 扩展名,并将 *.php 请求永久重定向到干净的 URL

经过大量的 Google 搜索后,我设法通过使用以下方法使漂亮的 URL 正常工作:

server {
    listen   80;
    server_name  www.url.com;
    rewrite ^/(.*) http://url.com/$1 permanent;
}

server {
    listen   80;
    server_name url.com;

    access_log /var/www/url.com/logs/access.log;
    error_log /var/www/url.com/logs/error.log;

    location / {

    root   /var/www/url.com/public/;
    index  index.html index.htm index.php;
    include /etc/nginx/mime.types;

    try_files $uri $uri/ @extensionless-php;

    }

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

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/url.com/public$fastcgi_script_name;
    }

    location /blog {
        root /var/www/url.com/public;
        index index.php;
        try_files $uri $uri/ /blog/index.php?$args;
    }

}

看起来似乎有效。但是,出于 SEO 目的,我想将请求从 url.com/filename.php 永久重定向到新的 url.com/filename

我知道我应该rewrite ^/(.*)\.php$ http://url.com/$1 permanent;在 .php 位置块中使用它,但是我该怎么做才不导致无限循环?

相关内容