导航首页 » 文章首页 » 站长技术 » 带问号的地址转发参数处理?宝塔Nginx重定向的参数问题

带问号的地址转发参数处理?宝塔Nginx重定向的参数问题

2023-02-23 247 站长技术

Nginx重定向的参数问题

    在给某网站写rewrite重定向规则时,碰到了这个关于重定向的参数处理问题。默认的情况下,Nginx在进行rewrite后都会自动添加上旧地址中 的参数部分,而这对于重定向到的新地址来说可能是多余。虽然这也不会对重定向的页面显示结果造成多少影响,但当你注意到新地址中包含有多余的 “?xxx=xxx”时,心里总还是会觉得不爽。而且可能影响到网站的搜索优化SEO。那么该如何来处理这部分的内容呢?看了下面两个简单的例子你就会明 白了。

例如:
把http://example.com/test.php?id=xxx 重定向到 http://example.com/xxx.html
把我郁闷了好久,最后在谷大神那里找到了一片文章解决了。

这是刚开始的规则:

if ($query_string ~* "id=(\d+)$") {
        set $id $1;
        rewrite /art_list\.php /article/category-$id.html permanent;
        rewrite ^/goods\.php /goods/$id.html permanent;
        rewrite ^/category\.php /products/$id.html permanent;
        rewrite ^/child_cat\.php /category/$id.html  permanent;
        rewrite ^/art\.php /article/$id.html permanent;
        rewrite ^/art_list\.php /article/category-$id.html permanent;
        rewrite ^/artid\.php  /help/$id.html permanent;
        rewrite ^/article\.php /affiche/$id.html permanent;
}


发现问题:
重定向的地址都是 xxx.html?id=xxx
最后我修改了参数:

if ($query_string ~* "id=(\d+)$") {
        set $id $1;
        rewrite /art_list\.php /article/category-$id.html? permanent;
        rewrite ^/goods\.php /goods/$id.html? permanent;
        rewrite ^/category\.php /products/$id.html? permanent;
        rewrite ^/child_cat\.php /category/$id.html?  permanent;
        rewrite ^/art\.php /article/$id.html? permanent;
        rewrite ^/art_list\.php /article/category-$id.html? permanent;
        rewrite ^/artid\.php  /help/$id.html? permanent;
        rewrite ^/article\.php /affiche/$id.html? permanent;
}

结果正常了。

    注意,关键点就在于“?”这个尾缀。重定向的目标地址结尾处如果加了?号,则不会再转发传递过来原地址的问号?后面的参数那部分。
    假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数自行补充来实现。
例如:
把http://example.com/test.php?para=xxx&p=xx 重写向到 http://example.com/new.php?p=xx
可以写成:

rewrite  ^/test.php  /new.php?p=$arg_p?  permanent;


[全文结束]

参考文章:
    原始的动态页面需要给个301永久重定向到静态页面上,以告诉搜索引擎将原始的页面的权重转到新的静态页面下。

if ($query_string ~* "id=(\d+)$") {
        set $id $1;
        rewrite ^/goods\.php /goods/$id.html permanent;
}

    这样重定向后发现 当输入/goods.php?id=254访问的时候会跳转到/goods/254.html?id=254下,
    后面看见搜索引擎的收录地址也添加了后面不必要的参数,必须去掉后面参数。那该怎么来处理呢?
例如:
把http://iermei.com/test.php?para=xxx 重定向到http://iermei.com/new

若按照默认的写法:rewrite ^/test.php(.*) /new permanent;
重定向后的结果是:http://iermei.com/new?para=xxx

如果改写成:rewrite ^/test.php(.*) /new? permanent;
那结果就是:http://iermei.com/new

    所以,关键点就在于“?”这个尾缀。假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数来实现。
例如:
    把http://iermei.com/test.php?para=xxx&p=xx 重写向到http://iermei.com/new?p=xx

    可以写成:

rewrite  ^/test.php  /new?p=$arg_p?  permanent;

来源:https://www.iermei.com/xq/2039.html

PS:关于判断获取参数

if ($query_string ~* "appId=(\d+)$") {
  set $id $1;
  rewrite ^/show/index/.*   /rewrite/htmlrewrite/$id.html;
}


上一篇
502

百首美女MV地址分享,MP4格式