【问题标题】:How to use mod_rewrite to rewrite dynamic url for search?如何使用 mod_rewrite 重写动态 url 进行搜索?
【发布时间】:2011-02-10 22:22:27
【问题描述】:

我有一个网上商店,里面有一个搜索栏。当我搜索时,我被转发到一个如下所示的链接:

webshopdomain.com/index.php?route=product/search&keyword=我的搜索词组

(嗯,更像是这样:webshopdomain.com/index.php?route=product/search&keyword=my%20search%20phrase)

我需要在 htaccess 中添加什么来使这个 url : webshopdomain.com/search/my%20search%20phrase 显示上述 url 的内容?

我一直在玩这个工具 (http://www.webconfs.com/url-rewriting-tool.php),但不明白如何抓取搜索词组并将其放入静态 url。

我也一直在谷歌上搜索,但由于缺乏正确的措辞,我没有找到答案。

谢谢:-)

编辑:

我可以用这个吗? (刚想好,要测试...)

Options +FollowSymLinks
RewriteEngine on
RewriteRule search/(.*) index.php?route=product/search&keyword=$1
RewriteRule search/(.*)/ index.php?route=product/search&keyword=$1

【问题讨论】:

    标签: .htaccess url mod-rewrite redirect rewrite


    【解决方案1】:

    为避免递归,需要查看request line

    RewriteCond %{THE_REQUEST} ^GET\ /index\.php\?route=product/search&keyword=([^&\ ]*)&*([^\ ]+)?
    RewriteRule ^index\.php$ /search/%1?%2 [L,R=301]
    

    【讨论】:

    • 嗨,谢谢 :-) 我试过这个,但它没有接缝工作。是否可以让整个 index.php?route=product/search&keyword= 成为 url 的一部分,然后 = 之后的所有内容都成为新 url 中的 ONE 参数?
    【解决方案2】:

    这很简单,但必须重新排列 htaccess 文件本身。

    RewriteRule ^search/(.*)$ index.php\?route=product/search&keyword=$1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2010-12-14
      • 2011-11-10
      • 1970-01-01
      相关资源
      最近更新 更多