htaccess替换查询参数中的字符 [英] htaccess replace characters in query parameter

查看:65
本文介绍了htaccess替换查询参数中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址,例如 http://www.example .com/?product = test& retailer = test& site = test

在这种情况下(存在产品参数的情况下),我删除了& site = test,但保留了其余的网址.

In this instance (where product parameter is present) I remove &site=test but leave the rest of the url untouched.

如果不存在产品参数,例如 http://www.example.com/?retailer=test&site=test

If product parameter isn't present, e.g. http://www.example.com/?retailer=test&site=test

我删除& site = test并将?retailer = test更改为/retailer/test,因此完整网址为

I remove &site=test and change ?retailer=test to /retailer/test so the full url would be http://www.example.com/retailer/test. I also only make this happen on the root domain.

我使用这些规则进行

# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]

# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]

在第二条规则中,当将URL重写为/reatiler/test时,在这种情况下,有可能将其更改为/retailer/test + test,我需要将其更改为/retailer/test-test也可能是/零售商/测试+测试+测试,该测试需要是/零售商/测试-测试-测试

on the second rule when the url is rewritten to /reatiler/test there is the possibility for this to be /retailer/test+test in this instance I need to change it to /retailer/test-test could also be /retailer/test+test+test which would need to be /retailer/test-test-test

对此的帮助将不胜感激

推荐答案

您可以使用N标志(更多信息

You could use the N flag (more info here):

RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [N,R=301]

请注意,这实际上是等效的,因为此处涉及重定向:

Note that this is actually equivalent, since a redirect is involved here:

RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]

所以最终您的htaccess应该看起来像这样

So finally your htaccess should look like this

# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]

# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]

RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]

这篇关于htaccess替换查询参数中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆