如何同时控制URL美和查询字符串?

时间:2013-11-07 15:39:53

标签: regex apache .htaccess mod-rewrite query-string

我在用户输入时或在网站的直接链接中使用类似此domain.com/application/controller/action/parameterN/valueN的网址。

但我知道,当我使用AJAXjQuery实施<form>时,我可能必须在将表单数据发送到服务器时使用query string

但是我实现htaccessfront controller的方式我无法检测到使用查询字符串。我能做什么?实现一种方法来读取查询字符串或在JavaScript中创建一个函数来将我的查询字符串转换为漂亮的URL?我该怎么做?

我的.htaccess文件:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?key=$1 [L]

我的php文件:

<?php
    class System{
        private $_url;
        private $_explode;
        public $_controller;
        public $_action;
        public $_params;

        public function  __construct(){
            $this->setUrl();
            $this->setExplode();
            $this->setController();
            $this->setAction();
            $this->setParams();
        }

        private function setUrl(){
            $_GET['url'] = (isset($_GET['url']) ? $_GET['url'] : 'index/index_action');
            $this->_url = $_GET['url'] .'/' ;
        }

        private function setExplode(){
            $this->_explode = explode( '/' , $this->_url );
        }

        private function setController(){
            $this->_controller = $this->_explode[0];
        }

        private function setAction(){
            $ac = (!isset($this->_explode[1]) || $this->_explode[1] == null || $this->_explode[1] == 'index' ? 'index_action' : $this->_explode[1]);
            $this->_action = $ac;
        }

        private function setParams(){
            unset( $this->_explode[0], $this->_explode[1] );
            array_pop( $this->_explode );

            if ( end( $this->_explode ) == null )
                array_pop( $this->_explode );

            $i = 0;
            if( !empty ($this->_explode) ){
                foreach ( $this->_explode as $val ){
                    if ( $i % 2 == 0 ){
                        $ind[] = $val;
                    }else{
                        $value[] = $val;
                    }
                    $i++;
                }
            }else{
                $ind = array();
                $value = array();
            }
            if( count($ind) == count($value) && !empty($ind) && !empty($value) )
                $this->_params = array_combine($ind, $value);
            else
                $this->_params = array();
        }

        public function getParam( $name = null ){
            if ( $name != null )
                return $this->_params[$name];
            else
                return $this->_params;
        }

        public function run(){
            $controller_path = CONTROLLERS . $this->_controller . 'Controller.php';
            if( !file_exists( $controller_path ) )
                die('Houve um erro. O controller nao existe.');

            require_once( $controller_path );
            $app = new $this->_controller();

                if( !method_exists($app, $this->_action) )
                    die('Esta action nao existe.');

            $action = $this->_action;
            $app->$action();
        }
    }

1 个答案:

答案 0 :(得分:1)

你需要QSA标志:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?key=$1 [L,QSA]

QSA(查询字符串追加)标志在通过重写规则添加新查询参数的同时保留现有查询字符串。

参考:Apache mod_rewrite Introduction