1

我在启用 mod_rewrite 的情况下获取 $_GET 变量时遇到问题。我有以下.htaccess:

RewriteEngine On
RewriteRule ^/?Resource/(.*)$ /$1 [L]
RewriteRule ^$ /home [redirect]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ /app.php?page=$1&query=$2 [L]

app.php 是:

<?php
require("controller.php");
$app = new Controller();

和 controller.php 是:

<?php
require("model.php");
require("router.php");

class Controller{

//--------Variables------------
    private $model;
    private $router;
//--------Functions------------

    //Constructor
    function __construct(){
        //initialize private variables
        $this->model = new Model();
        $this->router = new Router();

        $page = $_GET['page'];

        //Handle Page Load
        $endpoint = $this->router->lookup($page);
        if($endpoint === false) {
            header("HTTP/1.0 404 Not Found");
        }else {
            $this->$endpoint($queryParams); 
        }
    }
    private function redirect($url){
        header("Location: /" . $url);
    }

    //--- Framework Functions
    private function loadView($view){
        require("views/" . $view . ".php");
    }
    private function loadPage($view){
        $this->loadView("header");
        $this->loadView($view);
        $this->loadView("footer");
    }

    //--- Page Functions
    private function indexPage(){
        $this->loadPage("home");
    }
    private function controlPanel(){
        echo "Query was " . $code;
        /*
        if($this->model->set_token($code)){
            $user = $this->model->instagram->getUser();
        }else{
            echo "There was an error generating the Instagram API settings.";
        }
        */
        $this->loadPage("controlpanel");
    }
    private function autoLike(){
        $this->loadPage("autolike");
    }
    private function about(){
        $this->loadPage("about");
    }
}

因此,我可能拥有的 URL 示例是 /app.php?page=controlpanel&query=null,它将被重写为 /controlpanel。我遇到的问题是我有另一个页面将表单发送到 /controlpanel,从而导致 URL 像 /controlpanel?code=somecode。

我想做的是得到 $_GET['code'] 而我似乎无法做到这一点。任何人都可以帮忙吗?提前为一些代码转储道歉。

4

1 回答 1

4

改变

RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ /app.php?page=$1&query=$2 [L]

RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ /app.php?page=$1&query=$2 [L,QSA]

QSA是追加查询字符串

从文档

“当替换 URI 包含查询字符串时,RewriteRule 的默认行为是丢弃现有的查询字符串,并将其替换为新生成的查询字符串。使用 [QSA] 标志会合并查询字符串。”

于 2013-07-24T05:19:44.803 回答