【问题标题】:Apache mod rewrite rulesetApache mod 重写规则集
【发布时间】:2009-11-05 21:26:32
【问题描述】:

问题大致是—— 我需要这样的请求路径:

主机/var1/val1/var2/val2/var3/val3/...

像这样被覆盖:

host/index.php?var1=val1&var2=val2&val3=var3&...

有什么想法吗?

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    mod_rewrite 无法自行解析键/值对。最好的办法是让 index.php 帮助解析。

    您的 .htaccess 文件如下所示:

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Rewrite all other URLs to index.php/URL
    RewriteRule .* index.php/$0 [PT,L]
    

    然后你会在 index.php 的开头使用 PHP 来设置 $_GET 变量:

    $params = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
    
    for ($i=0; $i<count($params); $i+=2) {
        $_GET[$params[$i]] = $params[$i+1];
    }
    

    【讨论】:

      【解决方案2】:

      试试这些规则:

      RewriteRule ^([^/]+)/([^/]+)/(.+) /$3?$1=$2 [QSA,N]
      RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2 [QSA,L]
      

      但您最好使用 PHP 解决方案。

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 1970-01-01
        • 2017-08-31
        • 2015-07-26
        • 2010-11-13
        • 2012-05-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        相关资源
        最近更新 更多