php 版本 7.1 清理网址

php version 7.1 clean urls

我有一个 index.php 完全像这样

echo "<h2>index page</h2>";
$url=explode("/",$_SERVER['QUERY_STRING']);
echo "<pre>";
print_r($url);
print_r($_SERVER['QUERY_STRING']);
echo "</pre>";

和另一个,在同一个文件夹中,profile.php 具有相同的代码

echo "<h2>profile page</h2>";
$url=explode("/",$_SERVER['QUERY_STRING']);
echo "<pre>";
print_r($url);
print_r($_SERVER['QUERY_STRING']);
echo "</pre>";

我正在尝试使用干净的 url 构建一个非常简单的路线系统。 当url是这样的时候http://localhost/vas/aaa/bbb 我从 index.php 得到以下结果,没问题:

索引页 大批 ( [0] => aaa 1 => bbb ) aaa/bbb

printscreen image of index

但是,当我输入:http://localhost/vas/profile/john/21 时,我将配置文件作为 url 的第一部分包含在 url 中,我得到了:

个人资料页面 大批 ( [0] => )

printscreen image of profile

这意味着如果我不做任何类型的路由,运行 profile.php,结果是一个空的 url-array,没有最重要的预期参数,例如john/21。 为什么,路由作为功能嵌入到 php 7.* 中? 这是我的 .htaccess 文件:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php? [QSA,L]

我建议你使用 parse_url() 函数以及将所有请求路由到 index.php 的方法在阿帕奇

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

对您当前的.htaccess 的一些解释:

前两行设置了url重写的条件,在你的情况下,如果找不到url中指定的文件夹或文件,它将被重写:

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

然后重写规则指定传递的url应该作为查询字符串传输给index.php

RewriteRule ^(.*)$ index.php? [QSA,L]

因此,如果您没有其他 .htaccess 规则,当您调用此 url http://localhost/vas/profile/john/21 时,您不应该以 profile.php 结束,因为您没有t 在 url 中指定文件名。你应该在 index.php 中以你的 url 作为参数结束。

但是,如果文件夹 vas/profile/john/21/ 确实存在并且其中有一个 index.php,这就是将要调用的文件,但是因为重写不会应用,所以您将没有您的查询字符串。

这里可能发生了其他事情,其他 .htacces 规则将 /profile/ 重定向到 profile.php 或者什么,这说明你正确地到达了这个文件。额外的信息可能会有用..