.htaccess中的多个mod重写

时间:2010-12-29 14:44:00

标签: .htaccess mod-rewrite variables

我想要以下规则,但我似乎没有得到正确的设置。

<domain>/training-courses/

最后有或没有斜线,它应该去:

<domain>/?index.php?page=training-courses

并且对于每个变量之后我希望它的行为如下:

<domain>/training-courses/success/another-value/and-yet-another/

<domain>/?index.php?page=training-courses&val1=success&val2=another-value&val3=and-yet-another-value

如果无法选择无限制的前导变量,我想在页面变量之后至少有2个变量

这可能吗?以及如何解决这个问题?

到目前为止,我有这个:

RewriteEngine On

RewriteRule ^test/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)$ /test/index.php?pagina=$1&val1=$2

RewriteRule ^test/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2
RewriteRule ^test/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2

RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/$ /test/index.php?pagina=$1&val1=$2&val2=$3
RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)$ /test/index.php?pagina=$1&val1=$2&val2=$3

3 个答案:

答案 0 :(得分:0)

你是完全正确的,你不能以你想要的方式处理无限量的子文件夹,mod_rewrite只是不支持它。

但是,为了解决您的第一个问题,要进行尾随/可选,请将规则更改为以下内容:

RewriteRule ^test/([^/]*)/?$ /test/index.php?pagina=$1

RewriteRule ^test/([^/]*)/([^/]*)/?$ /test/index.php?pagina=$1&val1=$2

RewriteRule ^test/([^/]*)/([^/]*)/([^/]*)/?$ /test/index.php?pagina=$1&val1=$2&val2=$3

答案 1 :(得分:0)

经过数小时和数小时的碾压之后,它才会起作用。由于不支持无限变量,我可以使用除页面之外的两个变量(pagina)。我现在拥有的是:

RewriteEngine On

RewriteRule ^(test/images|test/css|test/js|test/lib)($|/) - [L]

RewriteRule ^test/(.*)/(.*)/(.*)/ test/index.php?pagina=$1&e=$2&t=$3
RewriteRule ^test/(.*)/(.*)/(.*) test/index.php?pagina=$1&e=$2&t=$3
RewriteRule ^test/(.*)/(.*)/ test/index.php?pagina=$1&e=$2
RewriteRule ^test/(.*)/(.*) test/index.php?pagina=$1&e=$2
RewriteRule ^test/(.*)/ test/index.php?pagina=$1
RewriteRule ^test/(.*) test/index.php?pagina=$1
RewriteRules的

工作除了第一个从其他rewriterules中排除文件夹的工作。通过工作我的意思是它实际上将URL重定向到index.php,但我不能通过$ _GET ['pagina']

获取PHP中的值

奇怪的是,这将是有效的,尽管我的喜好太有限了:

RewriteEngine On

RewriteRule ^(test/images|test/css|test/js|test/lib)($|/) - [L]

RewriteRule ^test/(.*)/ test/index.php?pagina=$1

答案 2 :(得分:0)

我想回到我自己的问题并回答它。有一段时间我正在使用我想要的东西。

首先我在我的htaccess中有这个:

<IfModule mod_rewrite.c>

    RewriteEngine On

    # existing folders or files
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.* - [L]

    # non-existing folders or files
    RewriteRule ^(.*)/? index.php?p=$1 [L]

</IfModule>

这允许始终访问真实文件。如果路径不存在,它将把整个路径传递给index.php。

在index.php中,我在顶部有以下代码:

// Split the URL in all its components
$request = parse_url(strip_tags($_GET['p']));
// Only take the path without any leading slashes
$path = rtrim($request['path'], '/');
// Split each variable from eachother
$path_array = explode("/", $path);

if(count($path_array)){
    // By default I select the first variable as the page I will include later on
    $page = $path_array[0];

    // Each other variable I will pass as $val[#nr] starting with 1. Just to be sure I'm going to strip tags and slashes
    for($i=1;$i<count($path_array);$i++){
        $path_array[$i] = strip_tags($path_array[$i]);
        $path_array[$i] = stripslashes($path_array[$i]);
        $val[$i] = $path_array[$i];
    }
}
else{
    // If $path_array doesn't contain multiple variables lets assume $path will contain the $page
    $page = $path; 
}

// If $page is empty let's take on the default page to include later on
if(empty($page)){
    $page = "home";
}

if(file_exists($page . ".php")){
    include($page . ".php");
}
else{
    echo "Page " . $page . ".php does not exist.";
}

给你一些可能性的例子:

domain.com/将包含home.php domain.com/product/将包含product.php domain.com/product/298833/将包含product.php并解析298833为$ val [1]; domain.com/product/298833/purple-candle将包含product.php并解析为298833 $ val [1];和紫色蜡烛为$ val [2];注意没有包含前导斜杠。没关系。

我愿意接受改进或提示。现在,当我提出这个问题时,这肯定是我正在寻找的答案。

相关问题