簡體   English   中英

在 NGINX 從 URI 中刪除.html 擴展,沒有重定向

[英]In NGINX remove .html extension from URI without redirect

我正在嘗試修改我的 NGINX 配置,以在將 URI 中的 .html 擴展名傳遞給我的基於 PHP 的 CMS 之前將其剝離。

換句話說,當訪客進入時:

http://www.example.com/foo.html

我希望將 URI 更改為 /;

http://www.example.com/foo

無需進行實際的瀏覽器重定向。 這在 Apache 中很容易實現,但我似乎無法破解 NGINX 中的螺母。 這是我的配置文件中似乎不起作用的內容。

    location ~ \.html {
            rewrite ^(/.*)\.html(\?.*)?$ $1$2 last;
    }

    location / {
        try_files $uri $uri/  /index.php;       
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass    127.0.0.1:9000;
        include         fastcgi_params;
        fastcgi_param   REQUEST_URI     $request_uri;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
    }

使用此代碼,PHP 獲取的 REQUEST_URI 仍然是 /foo.html。

我自己的問題的可能答案,或者至少是一種解決方法。

所以顯然 $request_uri 總是會包含原始 URI,而不是重寫 - 包含在 $uri 中。 因此,為了解決這個問題,我使用一個變量來存儲修改后的 URI 並將其傳遞給 PHP。 不過,我對這個解決方案並不感到興奮。

location ~ \.html {
            rewrite ^(/.*)\.html(\?.*)?$ $1$2 last;
    }

    location / {
        set $new_uri $uri
        try_files $uri $uri/  /index.php;       
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass    127.0.0.1:9000;
        include         fastcgi_params;
        fastcgi_param   REQUEST_URI     $new_uri;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM