Nginx rewrite arg with question mark address forwarding parameter processing? Parameter problems of Nginx redirection

Source: Internet
Author: User
Parameter problems of Nginx redirection

When writing rewrite redirection rules to a website, I encountered this parameter handling problem about redirection. By default, Nginx will automatically add the parameter portion of the old address after rewrite, which may be superfluous for the new address to redirect to. While this will not affect the results of redirected pages, you will always feel uncomfortable when you notice that the new address contains an extra "? Xxx=xxx". and may affect the search optimization seo of the website. So how to deal with this part of the content. Look at the two simple examples below and you'll see.

For example:
REDIRECT Http://example.com/test.php?id=xxx to http://example.com/xxx.html
Depressed me for a long time, finally found an article in the Valley of the Great God to solve.

This is just the beginning of the rule:
if ($query _string ~* "id= (\d+) $") {
Set $id $;
rewrite/art_list\.php/article/category-$id. HTML permanent;
Rewrite ^/goods\.php/goods/$id. htmlpermanent;
Rewrite ^/category\.php/products/$id. htmlpermanent;
Rewrite ^/child_cat\.php/category/$id. html permanent;
Rewrite ^/art\.php/article/$id. htmlpermanent;
Rewrite ^/art_list\.php/article/category-$id. html permanent;
Rewrite ^/artid\.php/help/$id. html permanent;
Rewrite ^/article\.php/affiche/$id. htmlpermanent;
}

Problem found:
The redirected addresses are all xxx.html?id=xxx.
Finally I modified the parameters:
if ($query _string ~* "id= (\d+) $") {
Set $id $;
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;
}
The result is normal.

Note that the key point is "?" This suffix. Redirect to the end of the destination address if you add a number, then you will not be forwarded to the original address of the question mark? The following parameter that part.
What if you want to keep a particular parameter? can use Nginx itself with the $arg_parameter parameters to be supplemented by self-implementation.
For example:
Rewrite the http://example.com/test.php?para=xxx&p=xx to http://example.com/new.php?p=xx.
Can be written as: rewrite ^/test.php/new.php?p= $arg _p? Permanent

[end of full text]

Reference article:
The original dynamic page needs to be permanently redirected to a static page for 301 to tell the search engine to redirect the original page's weight to the new static page.
if ($query _string ~* "id= (\d+) $") {
Set $id $;
Rewrite ^/goods\.php/goods/$id. htmlpermanent;
}
This redirect finds that when the input/goods.php?id=254 is accessed, it jumps to/goods/254.html?id=254,
Later see the search engine's ingest address also added the following unnecessary parameters, you must remove the following parameters. Then how to deal with it.
For example:
REDIRECT Http://examplecom/test.php?para=xxx to Http://examplecom/new
If you follow the default notation: Rewrite ^/test.php (. *)/new Permanent;
The result of the redirect is: http://examplecom/new?para=xxx
If rewritten as: Rewrite ^/test.php (. *)/new? Permanent
The result is: http://examplecom/new
So, the key point is ". "This suffix. What if you want to keep a particular parameter? The $arg_parameter parameter can be implemented with Nginx itself.
For example:
Rewrite the http://examplecom/test.php?para=xxx&p=xx to http://examplecom/new?p=xx.
Can be written as: rewrite ^/test.php/new?p= $arg _p? Permanent
Source: http://www.lc365.net/blog/b/15359/
PS: About judging Get parameters
if ($query _string ~* "appid= (\d+) $") {
Set $id $;
rewrite^/show/index/.*/rewrite/htmlrewrite/$id. html;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.