PHP中的QUERY_STRING处理

mest 发布于 2018-12-30 php 最后更新 2018-12-30 14:44 355 浏览

它可以通过.htaccess发送查询到另一个文件? 有点解释: index.php处理所有这样的页面:

switch($page) {
    case 'index':
        require_once START . '/includes/pages/index.php';
    break;
case 'another':
        require_once START . '/includes/pages/another.php';
    break;
       and so on...
在.htaccess中:
RewriteRule ^index$ index.php?page=index [L]
RewriteRule ^another$ index.php?page=another [L]
链接(例如localhost / 84ss7d41):
RewriteRule ^(.*)$ index.php?QUERY_STRING=$1 [L]
所以在/includes/pages/index.php中:
if ($_GET['QUERY_STRING']) {
$_SERVER['QUERY_STRING'] = $_GET['QUERY_STRING'];
$query_string = check_input($_SERVER['QUERY_STRING']);
    //checking/validating processing further...
如何使像这样的一个localhost / 84ss7d41工作方式与现在一样,但没有index.php?如果发生什么事情(文件意外删除),我试图分离我的“项目”,检查/验证数据将工作相同,没有主脚本,它就像:
if (main project file index.php/ || /includes/pages/index.php doesn't exist) {
  check/validate data with reserve.php
}

jalias 2018-12-30

赞同来自:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*) /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /reserve.php [L]
这会将与现有文件(例如图像)不匹配的所有请求重定向到index.php,然后您可以使用$_SERVER["REQUEST_URI"]来决定如何操作。这是前端控制器模式http://en.wikipedia.org/wiki/Front_Controller_pattern的实现 此外,如果apache无法找到index.php,则请求将转发到reserve.php 虽然,建立这种冗余不一定是“正确”的事情(如果reserve.php也失踪了怎么办?)。对index.php执行适当的访问控制可能是一种更好的方法。