Php 将codeigniter URL结构中的下划线替换为破折号
我有一个网站是使用codeigniter开发的。我的URL结构有大约4个部分和500多页。目前,我的URL有下划线(u),但我需要将其替换为短划线(-) 当前结构Php 将codeigniter URL结构中的下划线替换为破折号,php,codeigniter,Php,Codeigniter,我有一个网站是使用codeigniter开发的。我的URL结构有大约4个部分和500多页。目前,我的URL有下划线(u),但我需要将其替换为短划线(-) 当前结构 mywebsite.com/app/standalone_apps/high_runner_beta/download mywebsite.com/app/standalone-apps/high-runner-beta/download 必需的URL结构 mywebsite.com/app/standalone_apps/hig
mywebsite.com/app/standalone_apps/high_runner_beta/download
mywebsite.com/app/standalone-apps/high-runner-beta/download
必需的URL结构
mywebsite.com/app/standalone_apps/high_runner_beta/download
mywebsite.com/app/standalone-apps/high-runner-beta/download
我无法使用CI中的路由功能,因为需要重定向的链接太多
我确实试过解决办法
但它不起作用
有人能建议一种方法,我可以用破折号替换URL上的下划线。这对我来说很有用->创建一个php文件:my_Route.php 使用此代码并上载到“application/core”目录:
<?php class MY_Router extends CI_Router {
function set_class($class)
{
$this->class = str_replace('-', '_', $class);
}
function set_method($method)
{
$this->method = str_replace('-', '_', $method);
}
function set_directory($dir) {
$this->directory = $dir.'/';
}
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php'))
{
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->set_directory($this->directory . $segments[0]);
$segments = array_slice($segments, 1);
}
if (count($segments) > 0)
{
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php'))
{
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']);
$this->set_directory('');
$this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index');
return $x;
}
else
{
show_404($this->fetch_directory().$segments[0]);
}
}
}
else
{
// Is the method being specified in the route?
if (strpos($this->default_controller, '/') !== FALSE)
{
$x = explode('/', $this->default_controller);
$this->set_class($x[0]);
$this->set_method($x[1]);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
}
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
{
$this->directory = '';
return array();
}
}
return $segments;
}
// If we've gotten this far it means that the URI does not correlate to a valid
// controller class. We will now see if there is an override
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']);
$this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index');
return $x;
}
// Nothing else to do at this point but show a 404
show_404($segments[0]);
} }
您可以使用.htaccess进行此操作吗?如果是这样的话,请看一看。正如@swatkins所说,.htaccess是您需要拥有look@swatkins我将尝试那种解决办法。。就knw而言,$1-$2
是什么意思?那么在您上面的示例中,独立应用程序
是控制器的名称,high\u runner\u beta
函数的名称和下载
只是一个参数,对吗?我想了解您的结构。@LiveEn-the$1
和$2
是从正则表达式中捕获的。