mod_rewrite 附加字符串到查询

发布于 2024-12-25 12:01:07 字数 438 浏览 0 评论 0原文

我正在尝试转换此 URL:

http://localhost/?q=foo&s=search

对此:

http://localhost/?q=+foo&s=search (i add a plus sign before the user query)

我尝试过:

RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /?q=\+%1 [R=301,L,NE]

但这会生成:

http://localhost/?q=+++++++++++++++++++++foo

关于如何在查询之前添加加号的任何建议?

I'm trying to transform this URL:

http://localhost/?q=foo&s=search

on this:

http://localhost/?q=+foo&s=search (i add a plus sign before the user query)

I tried with:

RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /?q=\+%1 [R=301,L,NE]

But this generates:

http://localhost/?q=+++++++++++++++++++++foo

Any advice on how to add the plus sign before the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2025-01-01 12:01:07

两个快速/简单的选项:

  1. 在后端添加 + (PHP: $query = '+' . $_GET['q'];)
  2. 添加参数指示重定向和避免重定向。

    # 该示例未经测试,但想法是添加一个额外的 
    # 查询参数(rw=1)。当重定向返回时, 
    # 未应用重定向,因为“+”已经应用了 
    #放在开头。
    
    RewriteCond %{QUERY_STRING} !&rw=1$ [NC]
    RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
    RewriteRule ^(.*)$ /?q=\+%1&rw=1 [R=301,L,NE]
    

Two quick/easy options:

  1. Add the + in the backend (PHP: $query = '+' . $_GET['q'];)
  2. Add a param indicating the redirect and avoiding the redirect.

    # The example is untested, but the idea is to add an extra 
    # query parameter (rw=1). When the redirect comes back through, 
    # the redirect is not applied, as the '+' as already been 
    # put on the beginning.
    
    RewriteCond %{QUERY_STRING} !&rw=1$ [NC]
    RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
    RewriteRule ^(.*)$ /?q=\+%1&rw=1 [R=301,L,NE]
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文