【问题标题】:Issue with using index controller in url while using htaccess使用 htaccess 时在 url 中使用索引控制器的问题
【发布时间】:2014-09-22 23:03:17
【问题描述】:

在我的 codeigniter 项目中,我正在使用 htaccess 从 url 中删除 index.php 扩展名。现在,当我尝试调用索引文件夹中的函数/页面时,例如:http://domain.com/project/index/contactus,它显示404 Page Not Found Error

我的htaccess文件如下图:

RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.htaccess - [F]
RewriteRule (.*)(css\/)(.*)$ $2$3 [QSA,L]
RewriteCond %{request_uri} !^index\.php
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

索引控制器:

class Index extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form', 'url','timezone_helper'));
        $this->load->library('tank_auth');
        $this->load->library('session');
        timezone();
    }

    function index()
    { 
          $this->load->view('index/index');
    }
    function contactus()
    {
        $data['email']=$this->config->item('webmaster_email', 'tank_auth'); 
        $data['page']='index/contactus';
        $this->load->view('layout/template',$data);
    }

}

谁能帮我找到这个问题的解决方案?

提前致谢。

【问题讨论】:

  • 一些服务器配置使应用程序只能像index.php?/$1 这样工作,但您也可以尝试index.php/$1。告诉我们结果。另外,向我们展示来自控制器的代码。
  • 我将RewriteRule ^(.*)$ index.php?/$1 [QSA,L] 更改为RewriteRule ^(.*)$ index.php/$1 [QSA,L],但出现页面未找到错误。
  • 控制器呢?它在哪里?
  • 请检查我更新的问题。
  • 网址http://domain.com/project/index/index 有效吗?

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

为您的 .htaccess 试试这个:

 <IfModule mod_rewrite.c>
    RewriteEngine On

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    #RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]


    </IfModule>

    <IfModule !mod_rewrite.c>

    ErrorDocument 404 /index.php
</IfModule>

我在尝试使用 RewriteBase 时遇到了问题,所以我不再使用(除非我特别需要)。

另外,在 application/config/config.php 制作base_urlindex_page = ''

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多