【问题标题】:CodeIgniter URL with .htaccess带有 .htaccess 的 CodeIgniter URL
【发布时间】:2013-04-25 00:26:30
【问题描述】:

大家好,我在为 CodeIgniter 框架设置 .htaccess URL 修复时遇到问题。

我在名为“sites”的文件夹中启动了一个站点。

=> 问题:

**此网址完美运行:

http://127.0.0.1/sites/home/index/test

我想从 URL 中删除 /index/

目标网址应如下所示:

http://127.0.0.1/sites/home/test

我怎样才能获得这个网址?

附加信息:

我在自动加载中完成了 URL 帮助程序,效果很好,这是我的控制器中的代码

class Home extends CI_Controller {

public function index($var='')
{
   echo $var; //outputs -> test
}

}

.htaccess

RewriteEngine on

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteBase /sites
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteRule ^(.*)$ index.php/$1 [L]

【问题讨论】:

  • 在下面查看Youn的答案。您将需要创建一条包罗万象的路线。这将捕获到主控制器的所有请求并将它们重新路由到 index() 方法。

标签: .htaccess codeigniter url


【解决方案1】:

为此,您需要在 config/routes.php 中添加一个路由:

route['home/(:any)'] = "home/index";

【讨论】:

  • 为了使变量可以通过 index() 函数参数访问,您甚至可以这样做:route['home/(:any)'] = "home/index/$1";
  • 哇!这正是我所需要的,它就像一个魅力!这是一个简单而有效的解决方案。谢谢你,伙计!
猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 2014-09-24
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 2013-12-15
相关资源
最近更新 更多