php - 通过 .htaccess 将额外的 get 变量附加到 URL

标签 php regex .htaccess mod-rewrite opencart

我正在尝试将额外的 get 变量附加到 URL。我目前正在使用带有我购买的自定义主题的 OpenCart。然而,主题的开发者一直没有回应。

当我搜索产品时,它只会搜索标题。有一个用于搜索描述的复选框,但默认设置为“关闭”,我无法在代码中的任何地方找到将其附加或更改为 URL 的地方。只需在 URL 末尾添加 &description=true 即可。

我有:

index.php?route=product/search&search=123 

...我需要将描述附加到此,使其看起来像:

index.php?route=product/search&search=123&description=true

我最后的办法是修改 .htaccess,这样我就可以劫持搜索请求,比如说,并将其修改到最后。

我试过写一些,但无济于事。

RewriteRule ^/page /page?var=val [QSA]

我似乎无法弄清楚我需要如何编写它才能使其工作。有人可以给我指出正确的方向吗?

编辑 - 还在乱搞,现在我有:

RewriteRule index.php?route=product/search&search=(.*)  /index.php?route=product/search&search=$1&description=true [QSA]

它不会将它追加到原来的,但是当我手动添加 &description=true 时,它会添加另一个?于是就变成了:

index.php?route=product/search&search=123&description=true&description=true

最佳答案

您不能匹配 RewriteRule 中的查询字符串。像这样使用 RewriteCond:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^route=product/search&search=[^&]+$ [NC]
RewriteRule ^index\.php$ /$0?description=true [QSA,L,NC,NE,R=302]

RewriteCond %{QUERY_STRING} 检查您的查询字符串。在目标中,我们使用新参数 description=true 和附加现有查询字符串的 QSA

引用资料:

关于php - 通过 .htaccess 将额外的 get 变量附加到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40773905/

相关文章:

php - 带有 Rewrite 的 ErrorDocument 并且仍然获得Referer

php - 发送电子邮件后更改 Div 中的文本?

php - 您能否在一个查询中更新具有最高 ID 的行?

php - 显示 100 英里内的餐厅

JavaScript 用正则表达式 trim 换行符?

apache - .htaccess 对于 SPA,将传入请求重写为 index.html,静态资源除外

php - 如何从外部服务器播放 BLOB 中的音频流?

javascript - 使用正则表达式(或其他东西)的 jQuery 选择器

javascript - 正则表达式匹配时间范围

apache - .htaccess 重写参数未传递到目标页面