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 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题