douhao2011 2013-07-24 05:13
浏览 56
已采纳

使用mod_rewrite时获取$ _GET变量

I am having issues getting $_GET variables with mod_rewrite enabled. I have the following .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]

and app.php is:

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

and controller.php is:

<?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");
    }
}

So an example of a URL that I might have is /app.php?page=controlpanel&query=null which would be rewritten as /controlpanel. The problem I have is that I have another page which sends a form to /controlpanel resulting in a URL like /controlpanel?code=somecode.

What I am trying to do is get $_GET['code'] and I cannot seem to do this. Can anyone help? Apologies in advance for a bit of a code dump.

  • 写回答

1条回答 默认 最新

  • doujiu7704 2013-07-24 05:19
    关注

    Change

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

    to

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

    QSA is to append the query string

    From the docs

    "When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined."

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了