.htaccess 中的多个 mod 重写

发布于 2024-10-09 06:59:03 字数 1073 浏览 0 评论 0原文

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

/training-courses/

末尾有或没有斜杠都应该转到:

/?index.php?page=training-courses

,对于此后的每个变量 extra,我希望它的行为如下:

/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

I want the following rules but I don't seem to get the right setup.

<domain>/training-courses/

both with or without the slash at the end it should go to:

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

and for each variable extra after this I want it to behave like this:

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

to

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

If it's not possible to have the option for unlimited leading variables, i'd like to have at least 2 variables after the page variable

Is this possible? and how do I get this sorted out?

I have this so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-10-16 06:59:03

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

You're quite correct, you can't handle unlimited variables as sub-folders in the way you would like to, mod_rewrite just doesn't support it.

However, in answer to your first problem, to make the trailing / optional, change your rules to the following:

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
感情旳空白 2024-10-16 06:59:03

经过几个小时的努力,它根本行不通。由于不支持无限变量,除了页面(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,但我无法通过即 $_GET['pagina'] 获取 PHP 中的值,

奇怪的是这会工作,尽管对于我的喜好来说太有限了:

RewriteEngine On

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

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

After hours and hours of struggeling it just WON'T work. Since unlimited variables just isn't supported I'm ok with two variables besides the page (pagina). What I have now is this:

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

None of the RewriteRules work except from the first one which excludes folders from the other rewriterules. With working I mean it will actually redirect the URLs to index.php but I can't get the values in PHP through ie $_GET['pagina']

Strange thing is this WILL work, though is way too limited for my liking:

RewriteEngine On

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

RewriteRule ^test/(.*)/ test/index.php?pagina=$1
做个ˇ局外人 2024-10-16 06:59:03

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

首先,我的 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];请注意,不包括前导斜杠。不要紧。

总的来说,我愿意寻求改进或建议。现在,这绝对是我问这个问题时一直在寻找的答案。

I would like to come back to my own question and answer it. For a while now i'm using exactly what I want.

First off I have this in my 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>

This allows to access real files at all times. If the path does not exist it will pass the entire path to index.php.

In index.php I have the following code at the top:

// 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.";
}

To give you some examples of the posibilities:

domain.com/ will include home.php
domain.com/product/ will include product.php
domain.com/product/298833/ will include product.php and parse 298833 as $val[1];
domain.com/product/298833/purple-candle will include product.php and parse 298833 as $val[1]; and purple-candle as $val[2]; Notice the leading slash not being included. It does not matter.

I'm open for improvement or tips in general. For now this is definitely the answer I was looking for back when I asked this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文