dongqu4443 2010-08-23 21:36
浏览 30

有没有办法在.htaccess中进行某种正则表达式循环

hi at the minute I have a list of rules in my .htaccess file but if I need to add a new page i then need to edit this file again and add yet another rulles. HEre is a few

RewriteRule ^admin/(.*).html$ index.php?x=admin&y=$1
RewriteRule ^admin/project/(.*).html$ index.php?x=work&p=project&$1
RewriteRule ^work/(.*).html$ index.php?x=work&p=$1

Is there some way that I can have 1 rule that will work for all. Is ther ways to do loops so that for each additional /something/extra in the url it will add it in the redirect??

Thanks.

  • 写回答

1条回答 默认 最新

  • douyiyi5284 2010-08-24 13:19
    关注

    The simple answer is no it is not possible to create a RewriteRule that can cope with a dynamic amount of variables. Your only real option is to capture the basics and capture everything afterwards as a single chunk and parse it with PHP. It may be possible to improve on what you currently have there, it depends whether the x=work in your second rule is a typo, and how consistent your query string variables are...

    RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)\.html$ index.php?x=$1&y=$2
    

    This is the sort of pattern you might see in an MVC based system. But they usually have routing system to allow you to specify patterns to match (which is the same as having to add the RewriteRules in many ways).

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)