【问题标题】:Add slash and cut extension of file (nginx rules)添加斜线和剪切文件扩展名(nginx 规则)
【发布时间】:2016-06-08 19:35:31
【问题描述】:

我试图弄清楚如何在每个网址的末尾添加“/”。 并且这个 url 需要没有扩展名。 例如:

example.com/about.php; example.com/about.html => example.com/about/

我在配置 nginx 重写规则方面是假的。 这是我的配置:

server{
    root /usr/share/nginx/www/example.com;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name example.com www.example.com;

    client_max_body_size 20M;

    # BEGIN W3TC Browser Cache
    gzip on;
    gzip_types text/css text/x-component application/x-javascript application/json  application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text$;
    # END W3TC Browser Cache

    location /blog {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
            # try_files $uri $uri/ /index.php;
            try_files $uri $uri/ /blog/index.php?q=$uri&$args;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }



    location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css|json)$ {
            expires max;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location / {
        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.php index.html index.htm;
    }

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

}

我创建了一些解决方案,但我无法将它们组合起来,因为我是 nginx 的菜鸟。

【问题讨论】:

  • This answer 可能会对您有所帮助。
  • @RichardSmith 感谢您的回答!正在尝试实施...
  • @RichardSmith 同样的问题

标签: mod-rewrite nginx url-rewriting seo


【解决方案1】:

您需要像这样更改部分配置

location / {

                        rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent;
                        rewrite ^(/.*[^/])(?!/)$ $1/ permanent;


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

                }

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

第一行 rewrite ^(/.*[^/])\.(html|php)$ $1/ permanent; 将重定向所有扩展名为 .html 或 .php 的请求

rewrite ^(/.*[^/])(?!/)$ $1/ permanent;

将 / 添加到请求,其中 uri 没有 / 结束

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 2015-10-20
    • 2013-03-12
    • 1970-01-01
    • 2011-08-10
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多