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条)

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题