Nginx 重写:从带有参数的 URL 中删除 .html

Nic*_*_be 4 rewrite nginx

如何从带有参数的 url 中删除 .html?

例如:http : //www.domain.com/somepage.html?argument= whole& bunch=a-lot

到:

http://www.domain.com/somepage?argument=whole&bunch=a-lot

我试过了

    location / {
    index index.html index.php; 
            rewrite ^\.html(.*)$ $1 last;
            try_files $uri $uri/ @handler; 
            expires 30d; ## Assume all files are cachable
     }
Run Code Online (Sandbox Code Playgroud)

和一堆其他建议,但似乎无法让它发挥作用....

Tnx

Jap*_*Mul 6

像这样修改你的配置:

# rewrite html extensions
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;

location / {
    index index.html index.php;
    # this way nginx first tries to serve the file as an .html although it doesn't have the extension
    try_files $uri.html $uri $uri/ @handler;
}
Run Code Online (Sandbox Code Playgroud)

当然,您可以添加任何缓存设置等,但这应该足以删除 .html 部分。