apache mod_rewrite 为任意数量的可能性编写一个规则 [英] apache mod_rewrite one rule for any number of possibilities

查看:17
本文介绍了apache mod_rewrite 为任意数量的可能性编写一个规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个相当大的网站,我的 .htaccess 开始感觉有点臃肿,有没有办法替换我当前的系统 - 每个可以传递的可能数量的变量的一个规则,以一个可以解释不同数量输入的表达式?

I'm building a fairly large website and my .htaccess is starting to feel a bit bloated, is there a way of replacing my current system of - one rule for each of the possibile number of vars that could be passed, to one catch all expression that can account for varying numbers of inputs ?

例如我目前有:

RewriteRule ^([a-z]+)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?mode=$1&id=$2&$3=$4&$5=$6
RewriteRule ^([a-z]+)/([^/]*)/([^/]*)/([^/]*)$ /index.php?mode=$1&id=$2&$3=$4
RewriteRule ^([a-z]+)/([^/]*)$ /index.php?mode=$1&id=$2
RewriteRule ^([a-z]+)$ /index.php?mode=$1

第一个反向引用总是 mode 并且(如果还有的话)第二个总是 id,此后任何进一步的反向引用在输入名称和它的名称之间交替价值

the first backreference is always the mode and (if any more exist) the second is always id, thereafter any further backreferences alternate between the name of the input and its value

http://www.example.com/search
http://www.example.com/search/3039/sort_by/name_asc/page/23

我希望能够有一个表达式来优雅地处理所有输入.

I would love to be able to have one expression to gracefully handle all the inputs.

推荐答案

喜欢 Drupal:

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

然后使用类似这样的 php 代码处理脚本中的所有内容

And then handle all the stuff in your script using php code something like this

$pathmap = ();
if ($_GET["q"]){
    $path = split("/", $_GET["q"]);
    for ($i=0; $i+1<count($path); $i++){
        $pathmap[$path[$i]] = $path[$i+1];
        $i++;
    }
}

这篇关于apache mod_rewrite 为任意数量的可能性编写一个规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆