.htaccess仅当GET参数存在时才重定向

时间:2020-10-20 06:45:47

标签: apache .htaccess redirect mod-rewrite http-redirect

我的.htaccess文件中包含以下行:

Redirect 301 /folder1 https://www.example.com/folder2/file.php

这会将所有内容从/folder1重定向到https://www.example.com/folder2/file.php

我需要一个条件,仅在URL包含mykey= GET参数时才允许此重定向,否则请忽略此重定向命令。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您不能使用执行基本URI匹配的Redirect指令来执行此操作。

您将需要使用基于mod_rewrite的规则,例如:

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)mykey= [NC]
RewriteRule ^folder1(/|$) /folder2/file.php [R=301,L,NC]

确保在测试之前清除缓存。

参考:

答案 1 :(得分:0)

我终于找到了可行的解决方案:

RewriteCond %{REQUEST_URI} ^/folder1/
RewriteCond %{QUERY_STRING} mykey=
RewriteRule ^folder1\/$ /folder2/file.php$1 [R=301,L]