Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess中的多个mod重写_.htaccess_Mod Rewrite_Variables - Fatal编程技术网

.htaccess中的多个mod重写

.htaccess中的多个mod重写,.htaccess,mod-rewrite,variables,.htaccess,Mod Rewrite,Variables,我想要以下规则,但我似乎没有得到正确的设置 /培训课程/ 无论末尾是否有斜杠,都应转到: /?index.php?page=培训课程 对于这之后的每个额外变量,我希望它的行为如下: /培训课程/成功/另一个价值观/还有另一个价值观/ 到 /?index.php?page=培训课程&val1=成功&val2=另一个值&val3=还有另一个值 如果不可能有无限前导变量的选项,我希望在页面变量之后至少有2个变量 这可能吗?我该怎么解决这个问题呢 到目前为止,我有: RewriteEngine On

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

/培训课程/

无论末尾是否有斜杠,都应转到:

/?index.php?page=培训课程

对于这之后的每个额外变量,我希望它的行为如下:

/培训课程/成功/另一个价值观/还有另一个价值观/

/?index.php?page=培训课程&val1=成功&val2=另一个值&val3=还有另一个值

如果不可能有无限前导变量的选项,我希望在页面变量之后至少有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

你说得很对,你不能以你想要的方式处理无限变量作为子文件夹,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

经过一个又一个小时的苦苦挣扎,它根本不起作用。因为不支持无限变量,所以除了页面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
除第一个重写规则将文件夹从其他重写规则中排除外,所有重写规则均无效。通过工作,我的意思是它实际上会将URL重定向到index.php,但我无法通过ie$\u get['pagina']获得php中的值

奇怪的是,这会起作用,尽管对我来说太有限了:

RewriteEngine On

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

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

我想回到我自己的问题并回答它。一段时间以来,我一直在使用我想要的东西

首先,我的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];请注意,前导斜杠未包含在内。没关系


总的来说,我对改进或提示持开放态度。现在,这肯定是我问这个问题时想要的答案。

当我确实这样做时,我得到了一个内部服务器错误,但感谢关于后续slasha的提示。抱歉,我可能应该在发布之前做一个快速测试。