.htaccess 使用GET参数重定向htaccess URL

mccptt67  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(224)

我正在尝试使用.htaccess从www.example.com重example.com/products/category/subcat/name/?page=0到example.com/example-subpage
我的代码不工作:

RewriteCond %{REQUEST_URI}  ^/products/category/subcat/name$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]
ghhkc1vu

ghhkc1vu1#

问题可能是第一个条件中尾部斜杠的处理。您需要将它包含在条件中:

RewriteCond %{REQUEST_URI}  ^/products/category/subcat/name/$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]

我个人更喜欢一个稍微灵活一点的匹配,带有一个 * 可选 * 的尾随斜杠:

RewriteCond %{REQUEST_URI}  ^/products/category/subcat/name/?$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]

可以简化为:

RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^/?products/category/subcat/name/?$ https://example.com/example-subpage [R=301,L,QSD]

最后一点,如果添加了其他意外的参数,则为剩余条件添加一点灵活性也是有意义的:

RewriteCond %{QUERY_STRING} (?:^|&)page=0(?:&|$)
RewriteRule ^/?products/category/subcat/name/?$ https://example.com/example-subpage [R=301,L,QSD]

相关问题