dongshi9407 2010-11-19 00:42
浏览 32
已采纳

mod_rewrite规则

What rule translates urls like so?

host.com/plastic_toys/pink_barbie/
host.com/index.php?q=plastic+toys+pink+barbie

The number of components in the category and product name vary.

Edit:

I guess this rule does part of the job:

RewriteRule ^([^/]*)/([^/]*)$ /index.php?q=$1+$2 [L]

The problem with the above rule is that it does not take care of converting underscores to +, so it translates like this:

host.com/plastic_toys/pink_barbie/
host.com/index.php?q=plastic_toys+pink_barbie

I guess I have to delegate that conversion to PHP. Now is there a way to test if the rule actually does the conversion the way I imagine it would?

  • 写回答

3条回答 默认 最新

  • dongzg2006 2010-11-19 03:03
    关注

    First - I would suggest using the following rules:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)/?$ /index.php?q1=$1&q2=$2 [L,QSA]
    

    The RewriteCond each check that a valid file or directory does not exist (for instance if you want to have a static /help.html and it exists, or /FAQ/index.php and it exists then the RewriteRule will not fire.

    I've split out the parameters into 2 items - so you can treat them as separate parameters (if you want to treat it as 1 parameter just change it to /index.php?q=$1-$2 or some other separator that you want - I would say do not use + since you want to parse off that character anyway).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测