.htaccess重写GET变量

我有一个index.php,它处理所有路由index.php?page = controller(简体)只是为了与视图分离逻辑。


Options +FollowSymlinks

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [NC]

基本上: HTTP://localhost/index.php页=控制器? 要


http:// localhost / controller /


谁能帮我添加以下内容的重写


http:// localhost / controller / param / value / param / value(如此)


那将是:


http:// localhost / controller /?param = value&param = value


我无法与Rewriterule一起使用。


控制器可能如下所示:


    <?php

if (isset($_GET['action'])) {

 if ($_GET['action'] == 'delete') {

do_Delete_stuff_here();

}

}

?>

并且:


    <?php

if (isset($_GET['action']) && isset($_GET['x'])) {

 if ($_GET['action'] == 'delete') {

do_Delete_stuff_here();

}

}

?>


婷婷同学_
浏览 539回答 3
3回答

Smart猫小萌

我认为最好将所有请求重定向到index.php文件,然后使用php提取控制器名称和任何其他参数。与任何其他框架(例如Zend框架)相同。这是简单的课程,可以做您想要做的事情。class HttpRequest{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* default controller class&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; const CONTROLLER_CLASSNAME = 'Index';&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* position of controller&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $controllerkey = 0;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* site base url&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $baseUrl;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* current controller class name&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $controllerClassName;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* list of all parameters $_GET and $_POST&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $parameters;&nbsp; &nbsp; public function __construct()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // set defaults&nbsp; &nbsp; &nbsp; &nbsp; $this->controllerClassName = self::CONTROLLER_CLASSNAME;&nbsp; &nbsp; }&nbsp; &nbsp; public function setBaseUrl($url)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->baseUrl = $url;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; public function setParameters($params)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->parameters = $params;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; public function getParameters()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->parameters == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->parameters = array();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->parameters;&nbsp; &nbsp; }&nbsp; &nbsp; public function getControllerClassName()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->controllerClassName;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* get value of $_GET or $_POST. $_POST override the same parameter in $_GET&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param type $name&nbsp; &nbsp; &nbsp;* @param type $default&nbsp; &nbsp; &nbsp;* @param type $filter&nbsp; &nbsp; &nbsp;* @return type&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getParam($name, $default = null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($this->parameters[$name])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this->parameters[$name];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $default;&nbsp; &nbsp; }&nbsp; &nbsp; public function getRequestUri()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($_SERVER['REQUEST_URI'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return '';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $uri = $_SERVER['REQUEST_URI'];&nbsp; &nbsp; &nbsp; &nbsp; $uri = trim(str_replace($this->baseUrl, '', $uri), '/');&nbsp; &nbsp; &nbsp; &nbsp; return $uri;&nbsp; &nbsp; }&nbsp; &nbsp; public function createRequest()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $uri = $this->getRequestUri();&nbsp; &nbsp; &nbsp; &nbsp; // Uri parts&nbsp; &nbsp; &nbsp; &nbsp; $uriParts = explode('/', $uri);&nbsp; &nbsp; &nbsp; &nbsp; // if we are in index page&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($uriParts[$this->controllerkey])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // format the controller class name&nbsp; &nbsp; &nbsp; &nbsp; $this->controllerClassName = $this->formatControllerName($uriParts[$this->controllerkey]);&nbsp; &nbsp; &nbsp; &nbsp; // remove controller name from uri&nbsp; &nbsp; &nbsp; &nbsp; unset($uriParts[$this->controllerkey]);&nbsp; &nbsp; &nbsp; &nbsp; // if there are no parameters left&nbsp; &nbsp; &nbsp; &nbsp; if (empty($uriParts)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // find and setup parameters starting from $_GET to $_POST&nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; $keyName = '';&nbsp; &nbsp; &nbsp; &nbsp; foreach ($uriParts as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->parameters[$value] = '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $keyName = $value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->parameters[$keyName] = $value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // now add $_POST data&nbsp; &nbsp; &nbsp; &nbsp; if ($_POST) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($_POST as $postKey => $postData) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->parameters[$postKey] = $postData;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* word seperator is '-'&nbsp; &nbsp; &nbsp;* convert the string from dash seperator to camel case&nbsp; &nbsp; &nbsp;*&nbsp;&nbsp; &nbsp; &nbsp;* @param type $unformatted&nbsp; &nbsp; &nbsp;* @return type&nbsp;&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected function formatControllerName($unformatted)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (strpos($unformatted, '-') !== false) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $formattedName = array_map('ucwords', explode('-', $unformatted));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $formattedName = join('', $formattedName);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // string is one word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $formattedName = ucwords($unformatted);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // if the string starts with number&nbsp; &nbsp; &nbsp; &nbsp; if (is_numeric(substr($formattedName, 0, 1))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $part = $part == $this->controllerkey ? 'controller' : 'action';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception('Incorrect ' . $part . ' name "' . $formattedName . '".');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return ltrim($formattedName, '_');&nbsp; &nbsp; }}如何使用它:$request = new HttpRequest();$request->setBaseUrl('/your/base/url/');$request->createRequest();echo $request->getControllerClassName(); // return controller name. Controller name separated by '-' is going to be converted to camel case.var_dump ($request->getParameters());&nbsp; &nbsp; // print all other parameters $_GET & $_POST.htaccess文件:RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [NC,L]RewriteRule ^.*$ index.php [NC,L]

幕布斯6054654

您的重写规则将传递整个URL:RewriteRule ^(.*)$ index.php?params=$1 [NC]您的index.php将为您解释完整路径为controller / param / value / param / value(我的PHP有点生锈):$params = explode("/", $_GET['params']);if (count($params) % 2 != 1) die("Invalid path length!");$controller = $params[0];$my_params = array();for ($i = 1; $i < count($params); $i += 2) {&nbsp; $my_params[$params[$i]] = $params[$i + 1];}
打开App,查看更多内容
随时随地看视频慕课网APP