【问题标题】:Code Igniter always wants index.php to access the controllersCode Igniter 总是希望 index.php 访问控制器
【发布时间】:2016-05-19 01:19:14
【问题描述】:

我正在使用 codeigniter 开发一个简单的 php 项目。 我的 htaccess 是这样的

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

我的基本网址是

$config['base_url'] = 'http://localhost/Achalaweerasooriya.github.io';

我的索引页是

$config['index_page'] ="";

我的主控制器类是 LoginController。

当我运行项目时,它会调用登录控制器,并且 url 是

http://localhost/Achalaweerasooriya.github.io/index.php

这显示了登录表单。当我输入登录详细信息并单击登录时,它应该验证用户。我为登录按钮提供的 href 是

href="<?php echo base_url('');?>LoginController/validate"

然后url显示为

"http://localhost/Achalaweerasooriya.github.io/LoginController/validate"

这在逻辑上是正确的。

但是当我使用正确的用户名和密码单击登录按钮时,会生成 404 错误,说找不到对象 错误说 "找不到对象!

在此服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。

如果您认为这是服务器错误,请联系网站管理员。

错误 404

本地主机 Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.5.30"

但如果我手动将 index.php 添加到 url(然后 url 看起来像“http://localhost/Achalaweerasooriya.github.io/index.php/LoginController/validate”),则会显示正确的页面。 作为解决方案,我将 index.php 添加到基本 url 中,使其看起来像

$config['base_url'] = 'http://localhost/Achalaweerasooriya.github.io/index.php';

但是我使用了 base_url();调用链接外部 css 和 javascripts,它们停止响应。

请指出正确的方向。在此先感谢。

【问题讨论】:

  • 不要放 index.php $config['base_url'] = 'http://localhost/Achalaweerasooriya.github.io/';
  • 这不会改变任何东西。运行时它会自动获取 index.php
  • 你使用什么 wamp 或 xampp
  • 你可以尝试评论REQUEST_FILENAME这两个条件吗?
  • 确保您的 Apache 配置允许 .htaccess(检查 httpd.conf 中的 AllowOverride),否则您的 .htaccess 文件将被忽略

标签: javascript php apache .htaccess codeigniter


【解决方案1】:

如果您的 .htaccess 文件正常工作,则删除与 index.php 相关的旧代码行并添加以下代码

或者只是将 .htaccess 设为空白并尝试以下代码。它对我有用。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [QSA,L]
</IfModule>

【讨论】:

    【解决方案2】:

    从 $config['base_url'] 中删除 index.php 并试试这个

    http://addictors.in/remove-index-php-from-url-in-codeigniter/

    同时重启服务器

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2015-03-01
      • 2019-02-05
      • 2014-07-19
      • 1970-01-01
      相关资源
      最近更新 更多