如何正确制作位置并重写查询类型:
要求:
site.com/a/?f=qwerty
改写为:
/a/?f=qwerty
要求:
site.com/b/?g=qwerty
改写为:
/b/?g=qwerty
要求:
site.com/anyshorttext/?h=qwerty
改写为:
/special/?h=qwerty
我应用了这些解决方案:
server {
location ~ /a/?(.*)$ {
rewrite ^/([-\w]+) /a/?m=$1 break;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
location ~ /b/?(.*)$ {
rewrite ^/([-\w]+) /b/?m=$1 break;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
location ~* ^/[a-zA-Z0-9/_$/]+$ {
rewrite ^/([-\w]+) /special/?h=$1 break;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
}
}
但是,在这种情况下,请求转发到/特/只
谢谢你。
您的示例意味着查询字符串不会因每种情况而改变。如果是这样,您可以尝试以下操作:
map $uri $special_query {
# $special_query will be true, for paths like: //site.com/xyz/?stuff
~^/[^/]+/$ $query_string;
}
server {
location / {
if ($special_query) {
return 301 "/special/?$query_string";
}
}
# these locations will override the default / location
location = /a/ {}
location = /b/ {}
location = /special/ {}
}
此解决方案既不使用正则表达式位置,也不重写这两者,这两者都会给您的配置增加不必要的复杂性并破坏自然的 nginx 流程。(参见 nginx 作者 Igor Sysoev 的可扩展配置:https : //www.youtube.com/watch?v= YWRYbLKsS0I )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句