【问题标题】:.htaccess redirect error with query string change.htaccess 重定向错误,查询字符串更改
【发布时间】:2021-06-25 03:36:10
【问题描述】:

我的网址是:

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

任何想法将不胜感激。

【问题讨论】:

  • 您在这个.htaccess 中有更多规则吗?这些参数在查询字符串中可以按任何顺序排列吗?
  • 不,我必须保持它们的顺序,因为这是一个堆叠的过程。

标签: regex .htaccess url-rewriting


【解决方案1】:

对于您显示的示例,请尝试遵循 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]

【讨论】:

  • 感谢 @RavinderSingh13 我正在使用 htaccess.madewithlove.be 来测试操作,但它说 `%{THE_REQUEST} 是不受支持的字符串?
  • @Jay-UK,请不要在线网站测试您的规则,它们无法正常工作,请在您的 apache 服务器中测试,然后让我知道,干杯。
  • 感谢您的建议,现在很接近了!这进行了更改,但使用了将整个文件目录插入页面的 URL,即example.com/var/www/vhosts/example.com/…
  • @Jay-UK,当然,它现在已修复,请检查一次,如果这对您有帮助,请告诉我。确保在测试 URL 之前清除浏览器缓存。
  • 嘿,Ravindaer,太棒了。工作一种享受。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 2010-11-06
  • 2014-05-10
相关资源
最近更新 更多