重定向多个查询字符串

Redirect multiple query strings

我需要用查询字符串重定向一些 url。 url 文件名和查询字符串参数名称发生变化,但查询字符串值始终相同。我已经创建了一个适用于单个 url 的测试 htaccess 文件,但我希望有一个更优雅的方法来更改它,因为我有许多 url 需要更改。

htaccess 测试文件

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)*=0(&|$) [NC]
RewriteRule ^page1\.html$ page1.html?query1=1 [R=301, L, NC]

我有一个 url 这样的:

http://www.example.com/page1.html?query1=0
http://www.example.com/page2.html?query2=0
http://www.example.com/page3.html?query3=0

我需要将其更改为:

http://www.example.com/page1.html?query1=1
http://www.example.com/page2.html?query2=1
http://www.example.com/page3.html?query3=1

如有任何帮助,我们将不胜感激。

您可以在此处使用 regex OR 运算符。

如果query1query2等只是样本,实际上是不同的字符串,那么使用

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (?:^|&)(query1|query2|query3)=0(&|$) [NC]
RewriteRule ^((page1|page2|page3)\.html)$ ?%1=1 [R=301,NC,L]

如果参数确实有一个公共前缀(query)后跟一个序号,则使用

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} (?:^|&)(query[123])=0(&|$) [NC]
RewriteRule ^(page[123]\.html)$ ?%1=1 [R=301,NC,L]