apache 重写:如何检查空的或不存在的查询参数?

apache rewrite: how do I check for empty or not present query parameter?

最好用一个例子来解释:

- If I enter either URLs in my brower :
  - www.myhost.com/my-page.html?year=
  - www.myhost.com/my-page.html

- I then want to get redirected to  www.my-2nd-host.com/current-year/my-page.html

知道它是如何完成的吗?谢谢。

你可以这样使用 mod_rewrite:

RewriteEngine On
RewriteCond %{QUERY_STRING) !(^|&)year=[^&]+
RewriteRule ^/?my-page\.html$ https://www.my-2nd-host.com/current-year/my-page.html [R=302,L]

正则表达式 (^|&)year=[^&]+ 检查是否存在具有非空值的 year URL 参数。 ! 前缀然后 否定 表达式,因此当 URL 参数为空或根本不存在时它是成功的。