Как прописать правило в конфиге NGINX?

Подскажите пожалуйста, как подменить GET параметры в URL?
Есть например такой URL на сайте с GET ?tag=river

https://mydomain.ru/category/?tag=river

Как такие URL делать такими?
https://mydomain.ru/category/river/

Убирать эти символы ?tag=

Пробовал по разному,
if ($arg_tag) {
rewrite ^/(.*)$ /$arg_tag/? permanent;
}


И не работает никак, может не в то место вставляю

Сайт на VPS, NGINX.
Вот конфиг (ip и домен изменены для примера)

Как правильно написать правило что бы решить мою задачу? И В какое место в конфиге его вставить?

server {
    server_name mydomain.ru;
    listen 80.90.191.111:443 ssl ;

    ssl_certificate "/var/www/httpd-cert/mydomain.ru_2023-06-16-15-12_04.crt";
    ssl_certificate_key "/var/www/httpd-cert/mydomain.ru_2023-06-16-15-12_04.key";

    charset utf-8;

    gzip on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/css text/xml application/javascript text/plain application/json image/svg+xml image/x-icon;
    gzip_comp_level 7;

    set $root_path /var/www/mysite_domain_ru_usr/data/www/mydomain.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {
        index index.php index.html;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    }

# не работает вариант 

if ($arg_tag) {
rewrite ^/(.*)$ /$arg_tag/? permanent;
}


    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/mydomain.ru.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
     }


location ~ /\.ht {
        deny  all;
    }
    
    location ~* ^/core/ {
        deny  all;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpeg|avi|zip|gz|bz2|rar|swf|ico|7z|doc|docx|map|ogg|otf|pdf|tff|tif|wav|webp|woff|woff2|xls|xlsx)$ {
        try_files $uri $uri/ /index.php?$args;
        expires 365d;
    }

    location @fallback {
        fastcgi_pass unix:/var/run/mydomain.ru.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    include "/etc/nginx/fastpanel2-sites/mysite_domain_ru_usr/mydomain.ru.includes";
    include /etc/nginx/fastpanel2-includes/*.conf;


    error_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.error.log;
    access_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.access.log;
}


server {
     server_name mydomain.ru;

    listen 80.90.191.111:80;
    return 301 https://$host$request_uri;

    error_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.error.log;
    access_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.access.log;
}


server {
    server_name www.mydomain.ru  ;
    listen 80.90.191.111:80;
    listen 80.90.191.111:443 ssl ;

    ssl_certificate "/var/www/httpd-cert/mydomain.ru_2023-06-16-15-12_04.crt";
    ssl_certificate_key "/var/www/httpd-cert/mydomain.ru_2023-06-16-15-12_04.key";

    return 301 $scheme://mydomain.ru$request_uri;

    error_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.error.log;
    access_log /var/www/mysite_domain_ru_usr/data/logs/mydomain.ru-frontend.access.log;
}
  • Вопрос задан
  • 304 просмотра
Пригласить эксперта
Ответы на вопрос 3
@AUser0
Чем больше знаю, тем лучше понимаю, как мало знаю.
Ну так то вы правильно делаете, только не видно, как вы делаете в конфиге. Поэтому остаётся только посоветовать сделать в конфиге правильно, и сразу всё получится.
Ответ написан
@q2digger
никого не трогаю, починяю примус
когда nginx обрабатывает ваш урл, у него должна быть переменная $arg_ , в вашем случае $arg_tag , подставляйте ее в редирект и все получится.
вот хороший пример https://stackoverflow.com/questions/46516307/why-d...
Ответ написан
@dodo512
Если было так:
set $root_path /var/www/mysite_domain_ru_usr/data/www/mydomain.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {
        index index.php index.html;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    }

# не работает вариант 

if ($arg_tag) {
rewrite ^/(.*)$ /$arg_tag/? permanent;
}


    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/mydomain.ru.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
     }

Поменять так:
set $root_path /var/www/mysite_domain_ru_usr/data/www/mydomain.ru;
    root $root_path;
    disable_symlinks if_not_owner from=$root_path;

    location / {
        index index.php index.html;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    }


    location /category/ {
        if ($arg_tag) {
            rewrite ^ /category/$arg_tag/? permanent;
        }
        rewrite ^/([^/]+/)([^/]+)/$ /index.php?q=$1&tag=$2 last;
        rewrite ^/(.+)$ /index.php?q=$1 last;
    }


    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/mydomain.ru.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
     }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы