duanlu9557 2013-04-09 13:15
浏览 27
已采纳

使用多个变量清理URL

I plan to receive a number of urls. What mod rewrite rules would I use to make the following conversions for incoming urls:

http://domain.com/i/ => 
http://domain.com/index.php

http://domain.com/i/b/233223/c/23333 => 
http://domain.com/index.php?b=233223&c=23333

http://domain.com/i/dd/9844kjhf/fj/djP756/ee/kjhKJH => 
http://domain.com/index.php?dd=9844kjhf&fj=djP756&ee=kjhKJH

http://domain.com/r/ => 
http://domain.com/restore.php

http://domain.com/w/place/chicago =>
http://domain.com/withold.php?place=chicago

http://domain.com/w/ =>
http://domain.com/withold.php

Basically, the first portion after the domain corresponds to a page, the rest is a set of any number of parameters to be passed.

  • 写回答

2条回答 默认 最新

  • duanhai1455 2013-04-09 18:26
    关注

    Try:

    # If the URI is just /i/, rewrite to index.php
    RewriteRule ^i/?$ /index.php [L]
    
    # If the URI is /i/ plus some paths, rewrite the paths into query string and let rewrite engine loop
    RewriteRule ^i/([^/]+)/([^/]+)(/?.*)$ /i$3?$1=$2 [L,QSA]
    

    This will take as many path nodes as the internal recursion limit is set to, by default it's 10. That means by default you can have up to 9 sets of parameters unless you up this limit. So:

    http://example.com/i/a/1/b/2/c/3/d/4/e/5/f/6/g/7/h/8/j/9
    

    will first match the 2nd rule, and continue to loop:

    1. /i/b/2/c/3/d/4/e/5/f/6/g/7/h/8/j/9?a=1
    2. /i/c/3/d/4/e/5/f/6/g/7/h/8/j/9?b=2&a=1
    3. /i/d/4/e/5/f/6/g/7/h/8/j/9?c=3&b=2&a=1
    4. /i/e/5/f/6/g/7/h/8/j/9?d=4&c=3&b=2&a=1
    etc.
    

    until finally you're left with

    /i?j=9&h=8&g=7&f=6&e=5&d=4&c=3&b=2&a=1
    

    and the first rule gets applied and you should finally end up with:

    /index.php?j=9&h=8&g=7&f=6&e=5&d=4&c=3&b=2&a=1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题