gpt4 book ai didi

regex - mod重写的奇怪错误

转载 作者:行者123 更新时间:2023-12-02 03:34:13 25 4
gpt4 key购买 nike

我的 mod_rewrite 有一个奇怪的错误。

这是我的 htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?a=$1
RewriteRule ^(.*)/(.*)$ index.php?a=$1&b=$2
RewriteRule ^(.*)/(.*)/(.*)$ index.php?a=$1&b=$2&c=$3

当我在地址栏中输入时:

localhost/test/1/2

在我的 index.php 文件中我有:

echo '<pre>';
print_r($_GET);
echo '</pre>';

我希望看到的是:

Array
(
[a] => test
[b] => 1
[c] => 2
)

但我得到的结果是:

Array
(
[a] => index.php
[b] => 1
[c] => 2
)

我做错了什么?

最佳答案

If-Then-Else 逻辑,互斥(和最终)规则

有几个问题。

  • 在 mod-rewrite 中,一组条件仅适用于一个规则。但在这里,您正试图将它们应用于多个规则。
  • 您的规则并不相互排斥。 .* 匹配所有字符,因此它可以匹配所有三种模式。我们需要更具体的规则。
  • 您的规则不是最终的(需要L 标记)。

试试这个:

RewriteEngine On

# IF: Does the file or folder exist?
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d

# THEN leave unchanged, skip 3 rules
RewriteRule ^ - [L,S=3]

# ELSE (the file or folder does not exist)
RewriteRule ^([^/]+)/?$ index.php?a=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&b=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?a=$1&b=$2&c=$3 [L]

# FINALLY: leave unchanged
RewriteRule ^ - [L]

关于regex - mod重写的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com