【问题标题】:php version 7.1 clean urlsphp 7.1 版干净的网址
【发布时间】:2019-01-02 08:48:58
【问题描述】:

我有一个完全一样的 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,没有 - 最重要的 - 预期的参数,例如约翰/21。 为什么会这样,路由作为功能嵌入在 php 7.* 中? 这是我的 .htaccess 文件:

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

【问题讨论】:

  • 如果你要求mod_rewrite它的Apache问题,它与php任何版本无关。

标签: php clean-urls


【解决方案1】:

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

前两行设置了url重写的条件,如果没有找到url中指定的文件夹或文件,则会重写:

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

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

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

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

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

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

【讨论】:

    【解决方案2】:

    我建议你使用 parse_url() 函数以及将所有请求路由到 apache 中的 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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-21
      • 2011-06-25
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多