【问题标题】:Codeigniter redirect base_url to default controllerCodeigniter 将 base_url 重定向到默认控制器
【发布时间】:2018-02-08 15:21:44
【问题描述】:

在我的网站中,基础 ur 是

base_url = 'localhost/cl/'

当他们输入“localhost/cl/”时,我想重定向到“localhost/cl/index.php/welcome”。我使用 .htaccess 文件进行重定向,但它不起作用。我怎么能这样做?这是我在 .htaccess 中使用的

Redirect 301 / http://localhost/cl/index.php/welcome

更新

我找不到使用 htaccess 解决此问题的任何解决方案。我在默认控制器中放了一条 if 语句,将我重定向到期望页面。

if(current_url() == base_url()) {
    redirect(base_url().'index.php/welcome/');
}

感谢大家给我宝贵的时间。

【问题讨论】:

  • 你在config.php上设置了吗$config['base_url'] = 'http://localhost/cl/';
  • 是的,我在配置中设置了这个。但是当有人在浏览器上键入此内容时,我想重定向
  • 同样的事情你可以在config文件夹中使用'rount.php'而不使用htaccess文件来解决
  • @Ayyappaamara 我找不到任何重定向 base_url 的方法,当 ulr 有控制器或方法名称时它可以工作。
  • @aziz 你能分享你的 config.php 和 route.php 文件吗?那么我可以给你完美的解决方案。

标签: .htaccess codeigniter redirect


【解决方案1】:

【讨论】:

  • 没有路由base_url的选项,我想当我有一个控制器或方法时,我可以使用路由来重定向或重写url
【解决方案2】:

按照这些步骤解决您的问题。

.htaccess

中添加此代码
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|bootstrap|dist|plugins|css|js|images|robots\.txt)
RewriteRule ^(.*)$ index.php?$1 [L]

config.php

$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].'/ci/';

$config['index_page'] = '';

routes.php

$route['default_controller'] = 'welcome';// Your controller name as landing page.

如果它不起作用意味着你需要在Apache中启用mod_rewrite

一切顺利。

【讨论】:

  • 我按照您的指示更改了代码,但没有成功。
  • 它没有重定向,输入'localhost/ci'后我没有重定向到'localhost/ci/index.php/welcome'
  • 为什么index.php会出现在url中?您的项目或需求中是否需要index.php
  • 是的兄弟,index.php是必须的,
  • 我使用了这个并在我的问题中提到,它不起作用。它会生成一个很长的 url :'(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多