我需要在 NGINX 中重写 URL。我尝试了谷歌搜索中的许多示例,但都失败了。
/manager/?whmpca=order_process&a=add&pid=5&a=add¤cy=1&billingcycle=monthly
我需要将其重写为
/manager/cart/a/add/pid/5/currency/1/billingcycle/monthly/
或者类似的东西
/manager/pid/5/currency/1/monthly
编辑1: 我们是否可以简单地将 URL 中的“&”和“=”替换为“/”。这样,我们将得到类似
manager/whmpca/order_process/a/add/pid/5/a/add/currency/1/billingcycle/monthly
编辑2: 这是我的完整配置文件。
server {
server_name example.com www.example.com *.example.com 10.10.10.10;
listen 10.10.10.10:443 default ssl;
root /home/example/public_html;
index index.php index.htm index.html;
rewrite_log on;
#HMPRESS rewrite config
# location /manager {
# try_files $uri $uri/ /manager/?whmpca=$uri&$args;
# }
location ~ ^/manager/?$ {
return 302 /manager/cart/a/$arg_a/pid/$arg_pid/currency/$arg_currency/billingcycle/$arg_billingcycle;
}
## wordpress configuration
location = /favicon.ico {
log_not_found off; access_log off;
}
location / {
try_files $uri $uri/ /index.php$args;
}
## wordpress conf ends
include /etc/nginx/fastcgi_cache.conf;
include /etc/nginx/static-files.conf;
include /etc/nginx/exclusions.conf;
access_log /var/log/virtualmin/example.com_access_log;
error_log /var/log/virtualmin/example.com_error_log notice;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME /home/example/public_html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /home/example/public_html;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $https;
location ~ \.php(/|$) {
try_files $uri $fastcgi_script_name =404;
fastcgi_pass unix:/var/php-nginx/134783825911919.sock/socket;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
ssl_certificate /home/example/ssl.combined;
ssl_certificate_key /home/example/ssl.newkey.bak;
}
答案1
if ( $args ~ "whmpca=order_process&a=add&pid=(\d)&a=add¤cy=(\d)&billingcycle=monthly" ) {
set $pid $1;
set $cur $2;
}
rewrite /manager/cart/a/add/pid/$pid/currency/$cur/billingcycle/monthly/ redirect;
答案2
nginx 将各个查询参数存储到带有$arg_
前缀的变量中。
在这种情况下,您可以使用以下模式:
location ~ ^/manager/?$ {
return 302 /manager/cart/a/$arg_a/pid/$arg_pid/currency/$arg_currency/billingcycle/$arg_billingcycle;
}
但是,如果您需要根据whmpca
参数的值限制操作,则需要额外的限制。
这也要求每个请求都有这些参数。如果缺少一个参数,它就会中断。