首页 > 解决方案 > htaccess 重定向所有路由以 xxx 查询字符串开头

问题描述

我使用以下代码将(wordpress).htaccess的子域重定向到:Domain1Domain2

RewriteEngine On
RewriteCond %{HTTP_HOST} ^learn.Domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.learn.Domain1\.com$
RewriteRule ^(.*)$ "https\:\/\/\Domain2\.com\/$1" [R=301,L]

一切正常。

但是我的产品下载链接,该电子邮件给用户以供下载,开始于woocommerce

learn.Domain1.com/?download_file=xxxxxxxxxxxxxxxxxx

我使用此cond重定向,但下载 URL 除外:

RewriteCond %{REQUEST_URI} !^/?download_file=

但这不起作用!

我该如何解决?

标签: phpwordpress.htaccesswoocommerce

解决方案


你在这里有两个问题:

  • ^则表达式中的 表示“开始于”,并且您的 URI 不是由查询参数开始的
  • 文档中所述REQUEST_URI不包括请求参数,请尝试QUERY_STRING
RewriteCond %{QUERY_STRING} !^download_file=(.*)

注意:由于^,这仅在参数download_file是第一个时才有效,如果不是这种情况,您可以使用:

RewriteCond %{QUERY_STRING} !(?:^|&)download_file=(.*)

推荐阅读