Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 如何将一个或多个GET请求重定向到不同的目录或文件?_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

.htaccess 如何将一个或多个GET请求重定向到不同的目录或文件?

.htaccess 如何将一个或多个GET请求重定向到不同的目录或文件?,.htaccess,mod-rewrite,redirect,.htaccess,Mod Rewrite,Redirect,因此,我有一个带有index.php的站点,GET请求被发送到该站点(或root/) 例如,http://example.com/index.php?category=Birthday 或http://example.com/?category=Birthday 我希望能够301仅将这些特定请求重定向到新目录http://example.com/templates/?category=Birthday 我已经尝试在.htaccess文件?category=中匹配GET请求的一部分,但一旦它到达新

因此,我有一个带有index.php的站点,GET请求被发送到该站点(或root/)

例如,
http://example.com/index.php?category=Birthday

http://example.com/?category=Birthday

我希望能够301仅将这些特定请求重定向到新目录
http://example.com/templates/?category=Birthday

我已经尝试在.htaccess文件
?category=
中匹配GET请求的一部分,但一旦它到达新目录,就会导致无限循环。这让我很难理解正则表达式

所有其他帮助和教程都执行将所有请求匹配到新目录的任务,但我希望重定向仅在使用
?category=


我的.htaccess文件在其他方面为空,没有其他特殊重写。如果需要,我可以根据要求发布代码。谢谢

您可以在站点根目录中使用此规则。htaccess:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)category= [NC]
RewriteRule ^(index\.php)?$ /templates/ [L,NC,R=302]

QUERY\u字符串
将自动转入目标URI。

这绝对完美!如果使用任何其他GET参数,它甚至会将整个查询带到新位置,这非常好。非常感谢您的帮助@anubhava。我对
QUERY\u STRING
一无所知,但我很高兴现在知道了。