htaccess:从查询字符串中删除选定的键=值对 [英] htaccess: remove selected key=value pair from query string
问题描述
我一直在努力解决这个问题,但是.htaccess的功能却不尽如人意。我有以下示例查询字符串:
I've been trying to work my head around this but my .htaccess powers just aren't as good as I thought. I have this sample query string:
http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana
是否可以使用.htaccess来做到这一点:
Is it possible to do this using .htaccess:
- 检查是否存在
type = fresh
。如果不是,则重定向到页面index.php - 如果存在 type = fresh ,请将其删除,但保留其余的查询字符串
- Check if
type=fresh
exists. If not then redirect to page index.php - If type=fresh exists, remove it but retain the rest of the query string
推荐答案
您可以使用mod_rewrite来匹配查询字符串。在您的.htaccess文件中:
You can use mod_rewrite to match against the query string. In your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)type=fresh&?(.*)$
RewriteRule ^(.*)$ /$1?%1%2 [L,R]
如果有人输入在浏览器中http://mycoolsite.com/store.php?a=apples&type=fresh&b=香蕉,他们的浏览器将重定向到 http://mycoolsite.com/store.php?a=apples&b=banana 。如果您希望重定向在内部进行(因此浏览器的位置栏不会改变),请删除RewriteRule末尾方括号中的,R
。
This will make it so if someone enters http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana in the browser, their browser will get redirected to http://mycoolsite.com/store.php?a=apples&b=banana . If you want the redirect to happen internally (so the browser's location bar doesn't change), remove the ,R
in the brackets at the end of the RewriteRule.
这篇关于htaccess:从查询字符串中删除选定的键=值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!