在我的 .htaccess 文件中我有:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?ToDo=$1 [L,QSA]
当执行 print_r($_GET) 时,它输出
Array ( [ToDo] => index.php )
但是,当我将重写规则更改为
^([^/.]+)/?$ index.php?ToDo=$1 [L,QSA]
print_r 然后输出 $_GET 是一个空数组。有人可以向我解释为什么会这样吗?
最佳答案
让我们举个例子 URI /abc
:
你的第一个正则表达式:
^([^/]+)/?$
匹配/abc
并将其重写为:
/index.php?ToDo=abc
现在 mod_rewrite
引擎再次运行并匹配正则表达式 ^([^/]+)/?$
再次为 URI /index.php
并将其重写为:
/index.php?ToDo=index.php
你的第二个正则表达式:
^([^/.]+)/?$
工作正常,因为它不匹配 /index.php
URI。
最好的写法是这样的:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?ToDo=$1 [L,QSA]
如果请求是针对有效文件或目录,这两行 RewriteCond
将阻止重写。
关于php - 为什么 $_GET = index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24811240/