簡體   English   中英

如何編寫 NGINX 重寫,從 /filters/ 之后刪除 URL 中的所有內容

[英]How to write an NGINX rewrite which strips out everything from the URL after /filters/

我們正在運行一個 Symfony 應用程序,它有一個路由:domain.tld/main-category/category/

出於 SEO 目的,他們希望在 URL 中包含過濾器,如下所示:domain.tld/main-category/category/filters/price:0-100

所以基本上我們希望瀏覽器中的 URL 保持不變,但在 Symfony 中它應該去掉 /filters/ 之后的所有內容,包括 /filters/。 因此路由應保留為“domain.tld/main-category/category/”。

我試過這個確實有效,但是我需要它使用 Symfony 路線工作但無法讓它工作。

rewrite ^/.*/filters/.* /test.html last;

NGINX 配置:

location ~ ^/index\.php(/|$) {
    include fastcgi_params;
    fastcgi_pass php_project:9000;
    fastcgi_read_timeout 600s;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    internal;
}

免責聲明:這是一個猜測,可能不適合你

PHP index.php等應用經常使用REQUEST_URI參數來確定“路由”。 這在您的fastcgi_params文件中定義為:

fastcgi_param REQUEST_URI $request_uri;

一種解決方案是使用map指令來操縱此值。

例如:

map $request_uri $myroute {
    default                      $request_uri;
    ~*^(?<newroute>/.*)/filters/  $newroute;
}
server {
    ...
    location ~ ^/index\.php(/|$) {
        include fastcgi_params;
        fastcgi_pass php_project:9000;
        fastcgi_read_timeout 600s;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
    
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param REQUEST_URI $myroute;
        internal;
    }
}

請注意, map必須放在server塊之外。

您不需要編輯fastcgi_params文件,只需確保fastcgi_param語句出現在include語句之后的位置塊中。

暫無
暫無

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

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