在htaccess的查询字符串重写一个变量重写、字符串、变量、htaccess

2023-09-02 11:40:09 作者:三从四德.

我使用PHP脚本我的网站产生一个查询字符串在URL中。一位在查询字符串变量是:颜色=红色

我想使我的.htaccess文件中的规则重新写颜色=红色的所有实例颜色=蓝色

我没有机会改变原有的PHP脚本这样一个的.htaccess规则是必要的。

解决方案

  RewriteEngine叙述上
的RewriteCond%{QUERY_STRING} ^(*放大器; |)颜色=红色(安培; * |)$#搜索颜色=红色
重写规则^(。*)$ $ 1〜%1colour =蓝%2 [L,R = 301]#重定向到颜色=蓝色
 

这将重定向与颜色=红色的查询字符串的URL网址与颜色=蓝色

I use a PHP script with my site that produces a querystring in the URL. One of the variables in the querystring is: colour=red

js如何把一个变量写到字符串里

I would like to make a rule in my .htaccess file that re-writes all instances of colour=red to colour=blue.

I don't have access to change the original PHP script so a .htaccess rule is needed.

解决方案

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&|)colour=red(&.*|)$  # search colour=red
RewriteRule ^(.*)$ $1?%1colour=blue%2 [L,R=301]       # redirect to colour=blue

This will redirect URLs with colour=red in query-string to URLs with colour=blue