dousao8152 2012-09-19 19:00
浏览 46
已采纳

使用htaccess和mod_rewrite在URL中重写搜索词

Currently I have this rewrite rule in place and it works great:

RewriteRule ^search/([^/]+)/? search.php?term=$1 [NC,QSA]

When I go to mysite.com/search/foo it takes me to the proper search page for foo. what I am looking to do is make the opposite work as well so that if I visit mysite.com/search.php?term=foo I will be taken to mysite.com/search/foo. I think I need a 301 redirect somewhere but I'm not sure how to do it without creating an infinite loop and the other posts I've read so far haven't been much help.

  • 写回答

3条回答 默认 最新

  • dswmmvrg40957 2012-09-19 21:07
    关注

    Try:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /search\.php\?term=([^&]+)
    RewriteRule ^search\.php$ /search/%1/? [L,R=301]
    

    or (if you place these after the rule that you already have

    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteCond %{QUERY_STRING} ^term=([^&]+)$
    RewriteRule ^search\.php$ /search/%1/? [L,R=301]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)