代码之家  ›  专栏  ›  技术社区  ›  Greg

关于.htaccess中动态重写规则的帮助

  •  0
  • Greg  · 技术社区  · 14 年前

    我试图将路径(第一个和最后一个/)重写为一个查询字符串,将文件名重写为另一个

    例如:

    http://domain.com/users/admins/testuser.html 
    rewrites to
    http://domain.com/index.php?path=users/admins&file=testuser
    
    and
    
    http://domain.com/home.html
    rewrites to
    http://domain.com/index.php?path=&file=home
    
    and
    
    http://domain.com/settings/account.html
    rewrites to
    http://domain.com/index.php?path=settings&file=account
    

    非常感谢前两个答案,他们都是很好的答案,但我不知道该用哪一个! 从php本身解析路径有什么好处吗?反之亦然?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Gumbo    14 年前

    尝试此规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((.+)/)?([^/]+)\.[^/.]+$ index.php?path=$2&file=$3
    

    PHP’s parse_url pathinfo 为此:

    $_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $pathinfo = pathinfo(substr($_SERVER['REQUEST_URI_PATH'], 1));
    var_dump($pathinfo);
    

    索引.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule !^index.php$ index.php
    
        2
  •  1
  •   aularon    14 年前

    试试这个:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((\w+/)*)(\w+)\.html$ index.php?path=$1&file=$3