首页 > 解决方案 > .htaccess 重定向错误,查询字符串更改

问题描述

我有一个网址是:

http://example.com?country=uk&animal=dog&size=large

我希望将查询字符串更改为

http://example.com?country=uk&animal=cat&size=small

我已经在 .htaccess 测试仪中尝试过这个

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{QUERY_STRING} ^(.*[&?]|)animal=(dog)&size=(large)([&?].*|)$
RewriteRule ^(.*)$ $1?%1animal=cat&size=small[R=301,NC,L]

但是重写中的 & 不断显示为686!

http://example.com?country=uk&animal=cat686size=small

任何想法将不胜感激。

标签: regex.htaccessurl-rewriting

解决方案


使用您显示的示例,请尝试遵循 htaccess 规则。请确保在测试您的 URL 之前清除您的浏览器缓存。

##making engine one here.
RewriteEngine ON
##External redirect rule goes here. Please make sure to use correct back references to be used later on.
RewriteCond %{THE_REQUEST} \s/([^?]*)\?(country=)uk&(animal=)dog(&size=)large [NC]
##Applying redirection with 301 redirect here.
RewriteRule ^ /%1?%2uk&%3cat&%4small [R=301,L]

推荐阅读