沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

"Troubleshooting" Nginx: Parsing URL Parameters and Redirecting

Demo URL: https://demo.wdssmq.com/?id=2887

For the above address, you can use the following rules to redirect (301) to a pseudo-static address in Apache.

RewriteCond %{QUERY_STRING} ^id=(.+)$ # The reference here is %1 instead of $1
RewriteRule ^$ %{REQUEST_SCHEME}://%{HTTP_HOST}/post/%1.html [L,R=301]

As for Nginx, there are two variables, $query_string and $args, that can be used to obtain URL parameters, and the effect seems to be the same.

Note: The ? after $arg_id.html indicates that the original URL parameters are not carried after the redirect.

# page + post
if ($args ~* "id=\d+") {
    rewrite ^/$ $scheme://$host/post/$arg_id.html? permanent;
}

↑↑ The if statement in the above code uses regular expressions for matching, but does not capture the matching result. Instead, it directly obtains the corresponding value using $arg_id. Similarly, other parameters can also be obtained using $arg_parameter_name.

The demo address actually redirects twice, and the second time it redirects from the ID to the alias, which is implemented by the plugin [Version Assistant - Z-Blog Application Center].

2022-05-11:

When I wrote this record, I hadn't set up the md2zb system to publish articles yet. Then, because 301 is cached by the browser, when I cleared it, it also cleared the login status, resulting in a failed attempt to edit and publish the article.

The correct way to clear it is described in: 「Water Pit」Clearing 301 Cache for a Specific Website in the Browser_Computer Networking_沉冰浮水

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.