URL重写 - 查询字符串 [英] URL Rewriting - Query String
本文介绍了URL重写 - 查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的工作MVC应用程序中,我有以下格式的网址:
I work on an MVC application and I have urls in the following format:
http://127.0.0.1/PDO/PARTIE%20III/index.php?rt=index/connexion
http://127.0.0.1/PDO/PARTIE%20III/index.php?rt=index/nouveauMsg
我试图重写URL,这样记载:
I am seeking to rewrite the url so that it reads:
http://127.0.0.1/PDO/PARTIE%20III/index/connexion
http://127.0.0.1/PDO/PARTIE%20III/index/nouveauMsg
没有人有任何建议,以帮助我完成这个url_rewrite?
Does anyone have any suggestions to help me complete this url_rewrite?
这是路由器类解析的网址,并加载正确的观点和/或行动
That's the router Class that parses the URL and loads the correct view and/or action
<?php
class router {
/*
* @the registry
*/
private $registry;
/*
* @the controller path
*/
private $path;
private $args = array();
public $file;
public $controller;
public $action;
function __construct($registry) {
$this->registry = $registry;
}
/**
*
* @set controller directory path
*
* @param string $path
*
* @return void
*
*/
function setPath($path) {
/*** check if path i sa directory ***/
if (is_dir($path) == false)
{
throw new Exception ('Invalid controller path: `' . $path . '`');
}
/*** set the path ***/
$this->path = $path;
}
/**
*
* @load the controller
*
* @access public
*
* @return void
*
*/
public function loader()
{
/*** check the route ***/
$this->getController();
/*** if the file is not there diaf ***/
if (is_readable($this->file) == false)
{
$this->file = $this->path.'/error404.php';
$this->controller = 'error404';
}
/*** include the controller ***/
include $this->file;
/*** a new controller class instance ***/
$class = $this->controller . 'Controller';
$controller = new $class($this->registry);
/*** check if the action is callable ***/
if (is_callable(array($controller, $this->action)) == false)
{
$action = 'index';
}
else
{
$action = $this->action;
}
/*** run the action ***/
$controller->$action();
}
/**
*
* @get the controller
*
* @access private
*
* @return void
*
*/
private function getController() {
/*** get the route from the url ***/
$route = (empty($_GET['rt'])) ? '' : $_GET['rt'];
if (empty($route))
{
$route = 'index';
}
else
{
/*** get the parts of the route ***/
$parts = explode('/', $route);
$this->controller = $parts[0];
if(isset( $parts[1]))
{
$this->action = $parts[1];
}
}
if (empty($this->controller))
{
$this->controller = 'index';
}
/*** Get action ***/
if (empty($this->action))
{
$this->action = 'index';
}
/*** set the file path ***/
$this->file = $this->path .'/'. $this->controller . 'Controller.php';
}
}
?>
推荐答案
在您的文档根目录或Apache的配置文件使用此重写规则的的.htaccess
:
Use this rewrite rule in a .htaccess
in your document root or Apache config file:
RewriteEngine On
RewriteRule ^PDO/PARTIE%20III/(.*)$ PDO/PARTIE%20III/index.php?rt=$1 [L]
您可以在马丁·梅林的网站方便地测试重写规则。
You can test rewrite rules easily at Martin Melin's site.
有关详细信息: Apache的的mod_rewrite
模块。
For more information: Apache's mod_rewrite
module.
这篇关于URL重写 - 查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文